@@ -2519,24 +2519,14 @@ public function get_posts() {
25192519 * Ensure deterministic ordering to prevent duplicate records across pages.
25202520 * When multiple posts have the same value for a field, add ID as secondary sort to guarantee consistent ordering.
25212521 * Note: this is to circumvent a bug that is currently being tracked in https://core.trac.wordpress.org/ticket/44349.
2522+ *
2523+ * Use a blacklist approach: add ID as tie-breaker for all orderby fields except those that are
2524+ * already deterministic (ID itself, random ordering, or search relevance).
25222525 */
2523- $ fields_requiring_deterministic_orderby = array (
2524- 'post_name ' ,
2525- 'post_author ' ,
2526- 'post_date ' ,
2527- 'post_title ' ,
2528- 'post_modified ' ,
2529- 'post_parent ' ,
2530- 'post_type ' ,
2531- 'name ' ,
2532- 'author ' ,
2533- 'date ' ,
2534- 'title ' ,
2535- 'modified ' ,
2536- 'parent ' ,
2537- 'type ' ,
2538- 'menu_order ' ,
2539- 'comment_count ' ,
2526+ $ fields_excluding_deterministic_orderby = array (
2527+ 'ID ' ,
2528+ 'rand ' ,
2529+ 'relevance ' ,
25402530 );
25412531
25422532 $ orderby_array = array ();
@@ -2555,10 +2545,10 @@ public function get_posts() {
25552545
25562546 $ orderby_array [] = $ parsed . ' ' . $ this ->parse_order ( $ order );
25572547
2558- // Check if this field needs deterministic ordering
2559- if ( in_array ( $ _orderby , $ fields_requiring_deterministic_orderby , true ) ) {
2548+ // Check if this field should have deterministic ordering (not in blacklist).
2549+ if ( ! in_array ( $ _orderby , $ fields_excluding_deterministic_orderby , true ) ) {
25602550 $ needs_deterministic_orderby = true ;
2561- // Use the order from the array for ID tie-breaker
2551+ // Use the order from the array for ID tie-breaker.
25622552 $ id_tie_breaker_order = $ this ->parse_order ( $ order );
25632553 } elseif ( 'ID ' === $ _orderby ) {
25642554 $ has_id_orderby = true ;
@@ -2577,21 +2567,21 @@ public function get_posts() {
25772567
25782568 $ orderby_array [] = $ parsed . ' ' . $ query_vars ['order ' ];
25792569
2580- // Check if this field needs deterministic ordering
2581- if ( in_array ( $ orderby , $ fields_requiring_deterministic_orderby , true ) ) {
2570+ // Check if this field should have deterministic ordering (not in blacklist).
2571+ if ( ! in_array ( $ orderby , $ fields_excluding_deterministic_orderby , true ) ) {
25822572 $ needs_deterministic_orderby = true ;
25832573 } elseif ( 'ID ' === $ orderby ) {
25842574 $ has_id_orderby = true ;
25852575 }
25862576 }
25872577 }
25882578
2589- // Add ID as tie-breaker if needed and not already present
2579+ // Add ID as tie-breaker if needed and not already present.
25902580 if ( $ needs_deterministic_orderby && ! $ has_id_orderby ) {
25912581 $ orderby_array [] = "{$ wpdb ->posts }.ID " . $ id_tie_breaker_order ;
25922582 }
25932583
2594- // Build the final orderby string
2584+ // Build the final orderby string.
25952585 if ( empty ( $ orderby_array ) ) {
25962586 $ orderby = "{$ wpdb ->posts }.post_date " . $ query_vars ['order ' ] . ', ' . "{$ wpdb ->posts }.ID " . $ query_vars ['order ' ];
25972587 } else {
0 commit comments