-
Notifications
You must be signed in to change notification settings - Fork 3.1k
get_adjacent_post: Fix adjacent post navigation for posts with identical dates #10394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
get_adjacent_post: Fix adjacent post navigation for posts with identical dates #10394
Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this 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.
…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]>
Co-authored-by: Weston Ruter <[email protected]>
…st` function. improve code clarity by renaming the `$op` variable to `$comparison_operator`,
d057a91 to
c62d55d
Compare
|
Thanks for the quick feedback and help on this one @westonruter 🍺 I can't believe this bug has been alive for so long! |
There was a problem hiding this 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!
|
Thanks, folks! I'll keep this alive for a couple of days to give other folks a chance to |
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:WHERE p.post_date > 'current_date'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:
WHERE (p.post_date > 'current_date' OR (p.post_date = 'current_date' AND p.ID > current_id))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 inget_adjacent_post()Added unit tests in
tests/phpunit/tests/link/getAdjacentPost.phpandtests/phpunit/tests/url.php`Test Steps
npm run test:php -- --filter Tests_Link_GetAdjacentPostnpm run test:php -- --filter Tests_URLScreenshots
Given a post list (noticed the duplicate dates)
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.