Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#46929 closed enhancement (duplicate)

posts with the same post_date are not ordered properly

Reported by: robertklodzinski's profile robertklodzinski Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.1.1
Component: Query Keywords:
Focuses: Cc:

Description

When a list of posts has exactly the same post_date, then the post order is broken. Moreover, the same post can be displayed on another page. The ordering also differs for different values of posts_per_page.

Steps to reproduce (the issue can be seen also in wp-admin):

  1. create list of posts with the same post_date. You can manually change the date in the database. I stumbled upon this issue while cloning posts while using the Duplicate Post plugin to duplicate posts for testing. It may be a good idea to have unique titles to visually see the problem.
  2. See the results
  3. Change the number of posts per page and verify them (do so for different number of posts per page)

A workaround I used in my case was to order by post_date and additionally by ID (the higher ID, the newer DB record, so if a post has the same post_date, but higher ID, it should be also displayed higher on the list):

<?php
function fix_post_order($query) {
  $query->set( 'orderby', [ 'date' => 'DESC', 'ID' => 'DESC' ] );
}
add_action( 'pre_get_posts', 'fix_post_order' );

Probably such case would not happen in pure WP without touching the DB (that's why I'm not creating a defect ticket), but I believe additional order by ID (apart from post_date) would be a good idea, anyway.

Change History (2)

#1 @robertklodzinski
6 years ago

  • Summary changed from posts with the same post_date does not to posts with the same post_date are not ordered properly

#2 @SergeyBiryukov
6 years ago

  • Component changed from General to Query
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @robertklodzinski, welcome to WordPress Trac!

Thanks for the report, we're already tracking this issue in #44349.

Note: See TracTickets for help on using tickets.