Skip to content

Conversation

@ramonjd
Copy link
Member

@ramonjd ramonjd commented Oct 22, 2025

Note

I came across all these sorting bugs while looking at #10262. I'm not sure if I'm on the right track here, but it seems like a bug that needs to be fixed. 🤷🏻

Problem

When multiple posts have identical post_date values (e.g., when bulk publishing drafts), the next/previous post navigation doesn't work.

The navigation skips posts or behaves unpredictably because the WHERE clause uses strict inequality (> or <) which excludes posts with the same date.

The get_adjacent_post() function's WHERE clause uses:

  • Next post: WHERE p.post_date > 'current_date'
  • Previous post: WHERE p.post_date < 'current_date'

When posts have identical dates, these conditions never match, causing navigation to fail.

Proposed solution

Modified the WHERE clause to include ID-based fallback for posts with identical dates:

  • Next post: WHERE (p.post_date > 'current_date' OR (p.post_date = 'current_date' AND p.ID > current_id))
  • Previous post: WHERE (p.post_date < 'current_date' OR (p.post_date = 'current_date' AND p.ID < current_id))

This ensures deterministic ordering when posts have identical dates while maintaining backward compatibility.

Changes

Modified: src/wp-includes/link-template.php - Updated WHERE clause logic in get_adjacent_post()

Added unit tests in tests/phpunit/tests/link/getAdjacentPost.phpandtests/phpunit/tests/url.php`

Test Steps

  1. Create test scenario:
    • Publish at least one post (e.g., "Hello World!")
    • Create three draft posts (e.g., "Sample Post 1", "Sample Post 2", "Sample Post 3")
    • Select all three draft posts, then Bulk Actions > Edit
    • Change Status to "Published"
  2. Verify navigation:
    • View any of the recently published posts
    • Click "Previous Post" - should navigate to another post published at the same time
    • Click "Next Post" - should navigate to another post published at the same time
    • Continue navigating - should be able to navigate through all posts with identical dates
  3. Expected Results:
    • Previous/next navigation works correctly between all posts with identical dates
    • Navigation is deterministic and consistent
    • No posts are skipped during navigation
    • Navigation works in both directions
  4. Run tests:
    • npm run test:php -- --filter Tests_Link_GetAdjacentPost
    • npm run test:php -- --filter Tests_URL

Screenshots

Given a post list (noticed the duplicate dates)

Screenshot 2025-10-22 at 6 20 07 pm

Navigate through the posts:

Kapture.2025-10-22.at.18.21.03.mp4

Trac ticket: https://core.trac.wordpress.org/ticket/8107


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ramonjd ramonjd self-assigned this Oct 22, 2025
@ramonjd ramonjd added the bug label Oct 22, 2025
@github-actions
Copy link

github-actions bot commented Oct 22, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly, westonruter, andrewserong.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ramonjd ramonjd requested a review from peterwilsoncc October 22, 2025 08:14
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good. Just a couple nits left from me. It would be good to get someone else's eyes on this, but I really love the thorough tests.

ramonjd and others added 6 commits October 23, 2025 13:33
…ntical dates.

This update modifies the `get_adjacent_post` function to ensure deterministic ordering when multiple posts share the same date by incorporating the post ID in the SQL query. Additionally, new unit tests have been added to verify the functionality for posts with identical dates, ensuring correct navigation through adjacent posts.
…cent_post` function.

This change enhances the SQL query preparation by introducing a separate variable for the WHERE clause, improving readability and maintainability. The version number for the ID-based fallback feature has been updated to 6.9.0.
Remove $GLOBALS['post'] = $orig_post;

Co-authored-by: Weston Ruter <[email protected]>
…st` function.

 improve code clarity by renaming the `$op` variable to `$comparison_operator`,
@ramonjd ramonjd force-pushed the fix/get_adjacent_post-for-posts-with-same-date branch from d057a91 to c62d55d Compare October 23, 2025 02:33
@ramonjd
Copy link
Member Author

ramonjd commented Oct 23, 2025

Thanks for the quick feedback and help on this one @westonruter 🍺

I can't believe this bug has been alive for so long!

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing great for me, and the approach looks good and is consistent with the earlier discussions in the trac ticket 👍

but I really love the thorough tests.

Same, it gives a good sense of confidence in the change.

This LGTM, too.

I can't believe this bug has been alive for so long!

Gotta love fixing an ancient bug!

@ramonjd
Copy link
Member Author

ramonjd commented Oct 23, 2025

Thanks, folks! I'll keep this alive for a couple of days to give other folks a chance to bask in our glory test and review, then I'll get it committed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants