admin管理员组文章数量:1287593
I have the following query:
<?php
$args = array(
'hide_empty' => false,
'orderby' => 'title',
'order' => 'DESC'
);
$terms = get_terms( 'projets-location', $args );
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) { ?>
<h5 id="<?php echo $term->slug; ?>" class="filter-menu-item" data-filter=".<?php echo $term->slug; ?>">
<strong><?php echo $term->name; ?></strong>
</h5>
<?php }
} ?>
which shows all the taxonomy terms from the projets-location
taxonomy, I've added the orderby
and order
attributes above but STILL they're not displaying in alphabetical order at all, am I being stupid her or is there something I'm going wrong? Any suggestions would be greatly appreciated!
I have the following query:
<?php
$args = array(
'hide_empty' => false,
'orderby' => 'title',
'order' => 'DESC'
);
$terms = get_terms( 'projets-location', $args );
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) { ?>
<h5 id="<?php echo $term->slug; ?>" class="filter-menu-item" data-filter=".<?php echo $term->slug; ?>">
<strong><?php echo $term->name; ?></strong>
</h5>
<?php }
} ?>
which shows all the taxonomy terms from the projets-location
taxonomy, I've added the orderby
and order
attributes above but STILL they're not displaying in alphabetical order at all, am I being stupid her or is there something I'm going wrong? Any suggestions would be greatly appreciated!
4 Answers
Reset to default 4Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'projets-location',
'orderby' => 'name',
'order' => 'DESC'
) );
As per the WordPress Codex for get_terms
on this link
https://developer.wordpress/reference/functions/get_terms/, following terms fields are accepted in order_by
argument 'name', 'slug', 'term_group', 'term_id', 'id', 'description'
and you are using title
which is not in the accepted terms fields, so that might be the issue.
I didn't success to order by name using the orderby argument, so I just used a sort function ::
// order by name ASC - change > to < to order by DESC
function sortByName($a, $b) {
return $a->name > $b->name;
}
$terms = get_terms( 'projets-location', $args );
usort($subterms, 'sortByName');
foreach ( $terms as $term ) {
....
Try with wpdb
<?php
global $wpdb;
$terms = $wpdb->get_results( "
SELECT
t.name,
t.slug
FROM
{$wpdb->prefix}term_taxonomy AS tt
INNER JOIN
{$wpdb->prefix}terms AS t
ON t.term_id = tt.term_id
WHERE
tt.taxonomy = 'projets-location'
ORDER BY
t.name DESC
" );
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) { ?>
<h5 id="<?php echo $term->slug; ?>" class="filter-menu-item" data-filter=".<?php echo $term->slug; ?>">
<strong><?php echo $term->name; ?></strong>
</h5>
<?php }
} ?>
本文标签: wp queryOrder taxonomy terms in alphabetical order
版权声明:本文标题:wp query - Order taxonomy terms in alphabetical order 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741264090a2368145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论