-2

I am trying to sort by a custom taxonomy, which is a rating, in my WordPress loop, however it is not sorting by number. If I sort by order 'ASC' then it will put those without a rating at the top - otherwise no ordering either by 'ASC' or 'DESC'.

Any ideas please?

$number = 1;
    $args = array( 'posts_per_page' => -1, 'orderby'=> 'rating', 'order' => 'ASC' ); 
    $wpb_all_query = new WP_Query( $args );
    while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();

if (has_term( 'Roast Dinner', 'type')) { ?>
    <li><?php echo $number . '. '; $number++;?>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <span class="remove-link"><?php echo strip_tags(get_the_term_list( $post->ID, 'rating', '' )); ?></span>
        <span class="remove-link league-table-price hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'price', '' )); ?></span>
        <span class="remove-link league-table-priceincservice hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'priceincservice', '' )); ?></span>
        <span class="remove-link league-table-meat hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'meat', '' )); ?></span>
        <span class="remove-link league-table-tubeline hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'tubeline', '' )); ?></span>
        <span class="remove-link league-table-tubestation hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'tubestation', '' )); ?></span>
        <span class="remove-link league-table-areaoflondon hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'areaoflondon', '' )); ?></span>
        <span class="remove-link league-table-districtoflondon hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'districtoflondon', '' )); ?></span>
        <span class="remove-link league-table-zone hidden"><?php echo strip_tags(get_the_term_list( $post->ID, 'zone', '' )); ?></span>
    </li>
<?php } ?>

Edit - I did try the solution shown in Orderby custom field for custome post type however it provides a blank loop - no output.

Kind regards James

1

Please read the documentation. 'rating' is not a valid value for orderby. To sort by a custom field you need to use 'orderby' => 'meta_value' or 'meta_value_num'.

Note that a ‘meta_key=keyname‘ must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use ‘meta_value_num‘ instead for numeric values. You may also specify ‘meta_type‘ if you want to cast the meta value as a specific type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’, same as in ‘$meta_query‘.

So your query would be:

$args = array( 
    'posts_per_page' => -1, 
    'orderby'        => 'meta_value_num',
    'meta_key'       => 'rating',
    'order'          => 'ASC'
);

Note that items without a rating will be missing from this query. To include them please see this previous question and answer on the topic: Orderby meta_value only returns posts that have existing meta_key

| improve this answer | |
  • Thanks for the answer, however I did try this earlier on and it returns an empty loop. Any further suggestions? – iwillbeawebdeveloper Mar 18 '18 at 9:35
  • This is correct. Your problem is something else. I notice you're not specifying a post type. Are you querying posts or a custom post type? – Jacob Peattie Mar 18 '18 at 9:59
  • I am querying posts, but ordering by custom taxonomy that is set in the post. I hope that makes sense - I am no WordPress expert. – iwillbeawebdeveloper Mar 18 '18 at 10:05
  • Ahhhh - just realised I am trying to sort by custom taxonomy - not custom field as stated in my question. – iwillbeawebdeveloper Mar 18 '18 at 10:18
  • Well if you're using data for sorting, you should make it meta. It's not possible to sort by taxonomy term this way. See this question and answer wordpress.stackexchange.com/questions/14306/… – Jacob Peattie Mar 18 '18 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.