0

I am seeing some strange behaviour when combining a custom order with pagination when using WP_Query. The arguments for my query are as follows:

$args = array(
    'post_type'       => 'news',
    'posts_per_page'  => 10,
    'meta_key'        => 'news_date',
    'orderby'         => 'meta_value_num',
    'order'           => 'DESC',
    'paged'           => $my_page
);

The variable $my_page contains the current page number as extracted from the URL or set to 1 if not defined in the URL.

This query returns the correct number of results but the order is incorrect. Removing the paged parameter results in the correct order being achieved but obviously breaks my pagination.

Is there something I am doing wrong? Thanks.

  • What values has meta_key news_date? – Alexey Feb 5 '15 at 16:32
  • The values of news_date are dates stored YYYYMMDD e.g. for today it would be 20150205 – Michael Feb 5 '15 at 16:34
  • 1
    Could you provide SQL queries generetad by WP_Query for correct order case and for wrong order case. SQL query stored in request param of $wp_query. – Alexey Feb 5 '15 at 20:54
1

You want to orderby 'date' not by 'meta_value_num' because it is to order numerically. So if you were ordering items by their price, then you would want to use 'meta_value_num'. The meta_key 'news_date' is most likely in date format.

You should also cast the meta_value to DATE using 'meta_type' => 'DATE'.

$args = array(
    'post_type'       => 'news',
    'posts_per_page'  => 10,
    'meta_key'        => 'news_date',
    'meta_type'       => 'DATE'
    'orderby'         => 'meta_value',
    'order'           => 'DESC',
    'paged'           => (int)$my_page
);

If your getting the correct order, you could always calculate the offset yourself by multiplying paged * posts_per_page. Also make sure that your $paged variable is an integer if you grab it from a query string then it is not an Integer.

| improve this answer | |
  • orderby date will order by the post's date column, not their meta field. and dates are numeric, the only thing casting as date will do is invalidate queries with values that are out of the range of valid dates, like 2015-99-50. – Milo Feb 5 '15 at 18:03
  • 1
    Yes you are correct about the orderby, it does sort by the date in the post. My mistake. However there is nothing wrong with using 'meta_type' => 'DATE','orderby'=>'meta_value' if you stored your meta in date format, and orderby meta_value will sort it out, I ran 2 tests with randomly generated values for meta value dates, one test was the format YYYY-MM-DD, the other YYYYMMDD such as asked in the question. ( The YYYYMMDD format would most likely sort just as well by number or date type) – OnethingSimple Feb 5 '15 at 22:31
  • I thought that the dates were being stored as numbers in the format yyyymmdd. That's why I was ordering numerically. But on investigation I found they were being stored as yyyy/mm/dd and therefore casting the value to a date has worked. Thanks! – Michael Feb 6 '15 at 11:02

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.