Skip to content

Commit a204b0d

Browse files
committed
Enhance get_adjacent_post function to include ID-based fallback for posts with identical dates, ensuring deterministic ordering in adjacent post queries.
1 parent 03ff908 commit a204b0d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/wp-includes/link-template.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,14 +1977,15 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19771977
*
19781978
* @since 2.5.0
19791979
* @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
1980+
* @since n.e.x.t Adds ID-based fallback for posts with identical dates in adjacent post queries.
19801981
*
19811982
* @param string $where The `WHERE` clause in the SQL.
19821983
* @param bool $in_same_term Whether post should be in the same taxonomy term.
19831984
* @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
19841985
* @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
19851986
* @param WP_Post $post WP_Post object.
19861987
*/
1987-
$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );
1988+
$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE (p.post_date $op %s OR (p.post_date = %s AND p.ID $op %d)) AND p.post_type = %s $where", $current_post_date, $current_post_date, $post->ID, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );
19881989

19891990
/**
19901991
* Filters the ORDER BY clause in the SQL for an adjacent post query.
@@ -2000,12 +2001,13 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
20002001
* @since 2.5.0
20012002
* @since 4.4.0 Added the `$post` parameter.
20022003
* @since 4.9.0 Added the `$order` parameter.
2004+
* @since n.e.x.t Adds ID sort to ensure deterministic ordering for posts with identical dates.
20032005
*
20042006
* @param string $order_by The `ORDER BY` clause in the SQL.
20052007
* @param WP_Post $post WP_Post object.
20062008
* @param string $order Sort order. 'DESC' for previous post, 'ASC' for next.
20072009
*/
2008-
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
2010+
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order, p.ID $order LIMIT 1", $post, $order );
20092011

20102012
$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
20112013
$key = md5( $query );

0 commit comments

Comments
 (0)