@@ -1814,7 +1814,8 @@ function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy =
18141814 * Can either be next or previous post.
18151815 *
18161816 * @since 2.5.0
1817- * @since 6.9.1 Adds deterministic fallback for sort clause if not modified by a filter.
1817+ * @since 6.9.0 Introduce deterministic fallback based in IDs to account for date collisions.
1818+ * @since 6.9.1 Remove deterministic fallback for sites modifying the WHERE clause via a filter. See #64390.
18181819 *
18191820 * @global wpdb $wpdb WordPress database abstraction object.
18201821 *
@@ -1966,8 +1967,9 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19661967 $ join = apply_filters ( "get_ {$ adjacent }_post_join " , $ join , $ in_same_term , $ excluded_terms , $ taxonomy , $ post );
19671968
19681969 // Prepare the where clause for the adjacent post query.
1969- $ where_prepared_with_deterministic_fallback = $ wpdb ->prepare ( "WHERE (p.post_date $ comparison_operator %s OR (p.post_date = %s AND p.ID $ comparison_operator %d)) AND p.post_type = %s $ where " , $ current_post_date , $ current_post_date , $ post ->ID , $ post ->post_type ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $comparison_operator is a string literal, either '<' or '>'.
1970- $ where_prepared = $ wpdb ->prepare ( "WHERE p.post_date $ comparison_operator %s AND p.post_type = %s $ where " , $ current_post_date , $ post ->post_type );
1970+ $ where_prepared = $ wpdb ->prepare ( "WHERE p.post_date $ comparison_operator %s AND p.post_type = %s $ where " , $ current_post_date , $ post ->post_type ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $comparison_operator is a string literal, either '<' or '>'.
1971+ $ deterministic_where_prepared = $ wpdb ->prepare ( "WHERE (p.post_date $ comparison_operator %s OR (p.post_date = %s AND p.ID $ comparison_operator %d)) AND p.post_type = %s $ where " , $ current_post_date , $ current_post_date , $ post ->ID , $ post ->post_type ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $comparison_operator is a string literal, either '<' or '>'.
1972+
19711973 /**
19721974 * Filters the WHERE clause in the SQL for an adjacent post query.
19731975 *
@@ -1982,7 +1984,7 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19821984 * @since 2.5.0
19831985 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
19841986 *
1985- * @param string $where The `WHERE` clause in the SQL.
1987+ * @param string $where_prepared The `WHERE` clause in the SQL.
19861988 * @param bool $in_same_term Whether post should be in the same taxonomy term.
19871989 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
19881990 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
@@ -1992,9 +1994,11 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19921994
19931995 // Only force deterministic fallback if the where clause has not been modified by a filter.
19941996 if ( $ where === $ where_prepared ) {
1995- $ where = $ where_prepared_with_deterministic_fallback ;
1997+ $ where = $ deterministic_where_prepared ;
19961998 }
19971999
2000+ $ sort_prepared = "ORDER BY p.post_date $ order LIMIT 1 " ;
2001+
19982002 /**
19992003 * Filters the ORDER BY clause in the SQL for an adjacent post query.
20002004 *
@@ -2009,13 +2013,14 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
20092013 * @since 2.5.0
20102014 * @since 4.4.0 Added the `$post` parameter.
20112015 * @since 4.9.0 Added the `$order` parameter.
2016+ * @since 6.9.0 Adds ID sort to ensure deterministic ordering for posts with identical dates.
2017+ * @since 6.9.1 Remove deterministic fallback for sites modifying the SORT clause via a filter. See #64390.
20122018 *
20132019 * @param string $order_by The `ORDER BY` clause in the SQL.
20142020 * @param WP_Post $post WP_Post object.
20152021 * @param string $order Sort order. 'DESC' for previous post, 'ASC' for next.
20162022 */
2017- $ sort_prepared = "ORDER BY p.post_date $ order LIMIT 1 " ;
2018- $ sort = apply_filters ( "get_ {$ adjacent }_post_sort " , $ sort_prepared , $ post , $ order );
2023+ $ sort = apply_filters ( "get_ {$ adjacent }_post_sort " , $ sort_prepared , $ post , $ order );
20192024
20202025 // Only force deterministic sort if the sort clause has not been modified by a filter.
20212026 if ( $ sort === $ sort_prepared ) {
0 commit comments