Make WordPress Core


Ignore:
Timestamp:
10/26/2025 09:19:07 PM (13 months ago)
Author:
ramonopoly
Message:

get_adjacent_post: modify WHERE clause to include ID-based fallback to ensure deterministic ordering

Since WordPress 2.7, where multiple posts have identical post_date values (e.g., when bulk publishing drafts), the next/previous post navigation skips posts or behaves unpredictably. This is because the WHERE clause uses strict inequality (> or <) which excludes posts with the same date.

To ensure deterministic ordering, this commit modifies the WHERE clause to include ID-based fallback for posts with identical dates.

Props ramonopoly, westonruter, andrewserong.

Fixes #8107.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/url.php

    r60253 r61066  
    570570                }
    571571        }
     572
     573        /**
     574         * Test get_adjacent_post with posts having identical post_date.
     575         *
     576         * @ticket 8107
     577         * @covers ::get_adjacent_post
     578         */
     579        public function test_get_adjacent_post_with_identical_dates() {
     580                $identical_date = gmdate( 'Y-m-d H:i:s', time() );
     581
     582                // Create 3 posts with identical dates but different IDs.
     583                $post_ids = array();
     584                for ( $i = 1; $i <= 3; $i++ ) {
     585                        $post_ids[] = self::factory()->post->create(
     586                                array(
     587                                        'post_title' => "Identical Post $i",
     588                                        'post_date'  => $identical_date,
     589                                )
     590                        );
     591                }
     592
     593                // Test from the middle post (2nd post).
     594                $GLOBALS['post'] = get_post( $post_ids[1] );
     595
     596                // Previous post should be the 1st post (lower ID, same date).
     597                $previous = get_adjacent_post( false, '', true );
     598                $this->assertInstanceOf( 'WP_Post', $previous );
     599                $this->assertSame( $post_ids[0], $previous->ID );
     600
     601                // Next post should be the 3rd post (higher ID, same date).
     602                $next = get_adjacent_post( false, '', false );
     603                $this->assertInstanceOf( 'WP_Post', $next );
     604                $this->assertSame( $post_ids[2], $next->ID );
     605
     606                // Test from the first post.
     607                $GLOBALS['post'] = get_post( $post_ids[0] );
     608
     609                // Previous should be empty (no earlier posts).
     610                $previous = get_adjacent_post( false, '', true );
     611                $this->assertSame( '', $previous );
     612
     613                // Next should be the 2nd post.
     614                $next = get_adjacent_post( false, '', false );
     615                $this->assertInstanceOf( 'WP_Post', $next );
     616                $this->assertSame( $post_ids[1], $next->ID );
     617
     618                // Test from the last post.
     619                $GLOBALS['post'] = get_post( $post_ids[2] );
     620
     621                // Previous should be the 2nd post.
     622                $previous = get_adjacent_post( false, '', true );
     623                $this->assertInstanceOf( 'WP_Post', $previous );
     624                $this->assertSame( $post_ids[1], $previous->ID );
     625
     626                // Next should be empty (no later posts).
     627                $next = get_adjacent_post( false, '', false );
     628                $this->assertSame( '', $next );
     629        }
    572630}
Note: See TracChangeset for help on using the changeset viewer.