Support

Account

Home Forums General Issues Order by ACF

Solving

Order by ACF

  • I am having issues with ordering my posts by my ACF Star Rating. Does anyone know what I am doing wrong?

     $args = array(
            'taxonomy'     => 'club',
            'hide_empty'   => '0',
            'meta_key'      => 'star_rating',
            'orderby'     => 'meta_value_num',
            'order'       => 'DESC'
    
              );
        echo $meta_key;
        $terms = get_terms('club', $args);
    
  • You can’t order terms by a custom field.

    orderby for get_terms() only accepts one of the argument listed on the codex page https://codex.wordpress.org/Function_Reference/get_terms

    In order to do this you need to get the terms, then loop through the terms, get your custom field and add it to each term object and then build a PHP function to order them.

    I’ve done this a few times

    
    function my_sort_function($a, $b) {
        if ($a->star_rating == $b->star_rating) {
            return 0;
        } elseif ($a->star_rating < $b->star_rating) {
            return -1;
        } else {
            return 1;
        }
    }
    
    $terms = get_terms($taxonomy, $args);
    if ($terms) {
        foreach ($terms as $index => $term) {
            $terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
        }
        usort($terms, 'my_sort_function');
    }
    
  • Thank you John,

    As a beginner I am still not able to get it working this way. This is the code I have:

    In my functions.php file:

        function my_sort_function($a, $b) {
            if ($a->star_rating == $b->star_rating) {
                return 0;
            } elseif ($a->star_rating < $b->star_rating) {
                return -1;
            } else {
                return 1;
            }
        }

    On my home page:

    <?php 
    $args = array(
        'taxonomy'     => 'sock-club',
        'hide_empty'   => '0'
          );
    $terms = get_terms('sock-club', $args);
    if ($terms) {
        foreach ($terms as $index => $term) {
            $terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
        
            $term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
          
        }
        usort($terms, 'my_sort_function');
    }
    
    ?>

    Can you see what is wrong with my code?

  • 
    <?php 
    $args = array(
        'taxonomy'     => 'sock-club',
        'hide_empty'   => '0'
          );
    $terms = get_terms('sock-club', $args);
    if ($terms) {
        foreach ($terms as $index => $term) {
            $terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
          
        }
        usort($terms, 'my_sort_function');
        
        // create the list after the terms are sorted
        // need to loop through them again
        $term_list = '';
        foreach ($terms as $term) {
            $term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
        }
          
    }
    
    ?>
    
  • I wanted to note that in the above code “ussort” should be “usort”. I just spent 30 minutes trying to figure out why I couldn’t get this code to work until I finally noticed the typo.

  • sorry about that, I posted it wrong once and then it was copied over several times and wrong every time.

    I’ve corrected the type so hopefully know one else will trip over the same thing.

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

You must be logged in to reply to this topic.