Home Forums General Issues Order by date problem when same value


Order by date problem when same value

  • Hi there,

    I’m having a problem with sorting my posts by the value of the datepicker field. My custom date is saved in the yymmdd format.

    This is my working query to display my posts and sort them by the custom date:

    $wp_query = new WP_Query( array(
    	'post_type' 		  => 'project',
    	'meta_key' 			  => 'project_date',
    	'meta_query'      => array(
        'key'             => 'project_date',
        'type'            => 'date'
    	'orderby' 			  => 'meta_value_num',
    	'order' 				  => 'DESC',
    	'paged' 				  => $paged
    get_template_part('templates/loop', 'project');

    So far so good. Since the datepicker only saves a date and not a time, it can happen that 2 posts have the same date/value.

    Let’s say I have 9 pages with on every page 10 posts;
    If 2 posts have the same date but are both on page 4 for example, there is still no problem and both posts are displayed correctly.

    However, if one of them happens to be the 10th post on page 4, and the other one the 1st on page 5, the 10th post on page 4 is being displayed as the 1st post on page 5? Very strange…

    First I thought it was a caching problem, so I turned off my W3 Total Cache plugin completely, but that didn’t fix it.

    So, is this a bug? And can I somehow add a random time value to the meta_value_num so they never can have the same value?


  • If this is a bug it would be a WP bug. But looking at your query there are few things that stand out. One is that you have a meta_query that I don’t think you need and the second is that in that meta_query you are telling that the meta field is a date and I’m pretty sure that WP expects this to be in YYYY-MM-DD format, so your dates may be being read wrong. The last thing is that you are using orderby => meta_value_number.

    Try this for your query:

    $args = array(
        'post_type' => 'project',
        'meta_key' => 'project_date',
        'orderby'=> 'meta_value',
        'order' => 'DESC',
        'paged' => $paged
    $wp_query = new WP_Query($args);
    get_template_part('templates/loop', 'project');
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Order by date problem when same value’ is closed to new replies.