Home Forums ACF PRO Order terms in taxonomy query


Order terms in taxonomy query

  • Hello,

    I’m trying to order my taxonomy terms, and it does not work 🙂

    here is my code :

    <?php $the_problems = get_field('related_taxo'); ?>	
    <?php if( $the_problems ): ?>
    <?php foreach( $the_problems as $problem ): ?>
      <li><?php echo $problem->name; ?></li>
    <?php endforeach; ?>
    <?php endif; ?>	

    I would like to order the terms, by name or by count… is it possible ?

    I read the doc and added this in my functions.php, but with no result 🙁

    function my_taxonomy_query( $args, $field, $key ) {
        $args['orderby'] = 'count';
        $args['order'] = 'ASC';
        return $args;
    add_filter('acf/fields/taxonomy/query', 'my_taxonomy_query', 10, 3);

    What am I missing ?

    thanks !

  • Hi @anybodesign

    The “acf/fields/taxonomy/query” is used to query the taxonomies on the backend. If you want to sort it on the front-end, you need to sort it manually using array_multisort() like sorting a repeater. This page should give you more idea about it:

    So you can do it like this:

    $value = get_field('taxonomy', '76');
    $order = array();
    // populate order
    foreach( $value as $i => $row ) {
    	$order[ $i ] = $row->count;
    array_multisort( $order, SORT_ASC, $value );

    I hope this helps.

  • Thanks a lot James !
    I can order now the outpout on the front-end 🙂

    I update my code like this :

    $the_problems = get_field('related_taxo');
    $order = array();
    foreach( $the_problems as $i => $problem ) {
        $order[ $i ] = $problem->count;
    array_multisort( $order, SORT_ASC, $the_problems )


  • An alternative approach is to install, order the terms how you want via drag and drop and then the terms automatically appear in the new custom order.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Order terms in taxonomy query’ is closed to new replies.