Make WordPress Core

Ticket #47642: patch.47642.20201030.1

File patch.47642.20201030.1, 1.4 KB (added by ramon fincken, 6 years ago)

patch.47642.20201030.1

Line 
1Index: src/wp-includes/class-wp-query.php
2===================================================================
3--- src/wp-includes/class-wp-query.php (revision 49451)
4+++ src/wp-includes/class-wp-query.php (working copy)
5@@ -2325,6 +2325,34 @@
6 } elseif ( 'none' === $q['orderby'] ) {
7 $orderby = '';
8 } else {
9+
10+ // https://core.trac.wordpress.org/ticket/47642
11+ // Database engine fix to prevent strange (duplicate or missing) results in paginated resultsets
12+ // Test for possible duplicate (non-unique, numeric or datetime) DB keys, add the unique ID
13+ $force_ID = array( 'post_author', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified', 'post_modified_gmt', 'post_parent', 'comment_count' );
14+ if ( is_array( $q['orderby'] ) ) {
15+ $needs_ID = false;
16+ $has_ID = false;
17+ foreach ( $q['orderby'] as $_orderby => $order ) {
18+ if( in_array( $_orderby, $force_ID ) ) {
19+ $needs_ID = true;
20+ } else {
21+ if( $_orderby === 'ID' ) {
22+ $has_ID = true;
23+ break; // All done
24+ }
25+ }
26+ }
27+ if( $needs_ID && !$has_ID ) {
28+ $q['orderby']['ID'] = 'ASC';
29+ }
30+ } else {
31+ // Not an array, test as string
32+ if( in_array( $q['orderby'], $force_ID ) ) {
33+ $q['orderby'] .= ' ID'; // Add ID here, note the space
34+ }
35+ }
36+
37 $orderby_array = array();
38 if ( is_array( $q['orderby'] ) ) {
39 foreach ( $q['orderby'] as $_orderby => $order ) {