0

I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:

$args = array(
    'numberposts'   => 10,
    'post_type'     => 'nominees',
    'meta_query' => array(
             array(
               'key' => 'category',
               'value' => get_the_ID(),
               'compare' => '=',
             )
      ),
      'orderby' => 'meta_value_num',
      'order' => 'DESC',
      'meta_key' => 'count_nominees'
);

I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?

Thanks.

CritingZ
  • 388
  • 2
  • 16
  • 1
    Have you checked this?: https://wordpress.stackexchange.com/a/65772/4984 – Klian May 02 '18 at 06:58
  • you can check this as well : https://stackoverflow.com/questions/10016568/wordpress-custom-field-search-results-sorted-by-value – Chintan May 02 '18 at 06:59
  • @Klian well, mine is the same, the problem is that my field is numeric – CritingZ May 02 '18 at 07:06
  • @Chintan yes, but this is different than my case – CritingZ May 02 '18 at 07:06
  • try sorting the result together with id or something else best suited, to remove the fluctuations of results for same count of votes. https://stackoverflow.com/questions/10016568/wordpress-custom-field-search-results-sorted-by-value – Aman Agarwal May 02 '18 at 07:09

1 Answers1

1

As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.

$args = array(
    'numberposts'   => 10,
    'post_type'     => 'nominees',
    'meta_query' => array(
        array(
            'key' => 'category',
            'value' => get_the_ID(),
            'compare' => '='
        )
    ),
    'meta_key' => 'count_nominees',
    'orderby' => array(
        'count_nominees' => 'DESC',
        'ID' => 'DESC'
    )
);

That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.

You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.

  • This did work, but I am surprised it works without **meta_value_num** , because last time I checked, when sorting by a custom field, it requires to use NUM in order to get the proper sorting, hmm, so I am a little bit confused – CritingZ May 02 '18 at 07:23
  • I think it's because it's looking for `meta_value_num` as a column name which doesn't exist (at least by default) so it's just skipping over the order clause. Glad it got things working though! –  May 02 '18 at 07:26