mrschk
mrschk

Reputation: 33

WordPress WP_Query results not properly sorted by date?

I'm trying to sort all posts by date, using the following code:

  $args = array(
    'post_type' => array( 'post' ),
    'orderby' => 'date',
    // 'orderby' => 'post_date',
    'order' => 'DESC',
    'post_status' => 'publish',
    'posts_per_page' => -1,
  );
  // wp_reset_query();
  $query = new wp_query( $args );

  while ( $query->have_posts() ) : $query->the_post();
    var_dump($query->post->post_date);
  endwhile;

Based on the above code, this is how results are now being output. Notice how the first four results are sorted, and starting from the fifth result it resorts again. How is that possible?

HTML output

I dumped the request, here's the SQL query: SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC

When running the above query in the database, I get the following (properly sorted) results:

DB SQL query

At first I thought it must be some interfering plugin or code in functions.php. So I completely emptied the functions.php file and disabled all plugins, but the result remained the same.

I have no clue what's causing this, can anyone help me?

Upvotes: 1

Views: 580

Answers (1)

mrschk
mrschk

Reputation: 33

Thanks to @DubVader, the issue was "sticky posts".

Problem solved by adding 'ignore_sticky_posts' => true to the query arguments.

Upvotes: 2

Related Questions