Support

Account

Home Forums General Issues Order by ACF Reply To: Order by ACF

  • 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?