Ticket #26937: 26937.patch
File 26937.patch, 6.0 KB (added by , 6 years ago) 


wpincludes/linktemplate.php
1128 1128 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 1129 1129 */ 1130 1130 function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 1131 global $wpdb; 1132 1133 if ( ( ! $post = get_post() )  ! taxonomy_exists( $taxonomy ) ) 1131 if ( ( ! $post = get_post() )  ! taxonomy_exists( $taxonomy ) ) { 1134 1132 return null; 1133 } 1135 1134 1136 $current_post_date = $post>post_date;1135 $current_post_date = get_post_field( 'post_date', $post ); 1137 1136 1138 $join = ''; 1139 $posts_in_ex_terms_sql = ''; 1140 if ( $in_same_term  ! empty( $excluded_terms ) ) { 1141 $join = " INNER JOIN $wpdb>term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb>term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; 1137 // Query basics 1138 $args = array( 1139 'posts_per_page' => 1, 1140 'post_status' => 'publish', 1141 'post_type' => 'post', 1142 'orderby' => 'date', 1143 'order' => $previous ? 'DESC' : 'ASC', 1144 'no_found_rows' => true, 1145 'cache_results' => true, 1146 'suppress_filters' => false, 1147 ); 1142 1148 1143 if ( $in_same_term ) { 1144 if ( ! is_object_in_taxonomy( $post>post_type, $taxonomy ) ) 1145 return ''; 1146 $term_array = wp_get_object_terms( $post>ID, $taxonomy, array( 'fields' => 'ids' ) ); 1147 if ( ! $term_array  is_wp_error( $term_array ) ) 1148 return ''; 1149 $join .= $wpdb>prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy ); 1149 // Query pieces 1150 $tax_query = array(); 1151 1152 // Set up for requests limited to posts that share terms 1153 if ( $in_same_term ) { 1154 $terms = get_the_terms( get_the_ID(), $taxonomy ); 1155 1156 if ( is_array( $terms ) && ! empty( $terms ) ) { 1157 $terms = wp_list_pluck( $terms, 'term_id' ); 1158 $terms = array_values( $terms ); 1159 $terms = array_map( 'intval', $terms ); 1160 } else { 1161 unset( $terms ); 1150 1162 } 1163 } 1151 1164 1152 $posts_in_ex_terms_sql = $wpdb>prepare( "AND tt.taxonomy = %s", $taxonomy ); 1153 if ( ! empty( $excluded_terms ) ) { 1154 if ( ! is_array( $excluded_terms ) ) { 1155 // backcompat, $excluded_terms used to be $excluded_terms with IDs separated by " and " 1156 if ( false !== strpos( $excluded_terms, ' and ' ) ) { 1157 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 1158 $excluded_terms = explode( ' and ', $excluded_terms ); 1159 } else { 1160 $excluded_terms = explode( ',', $excluded_terms ); 1161 } 1165 // Handle excluded terms 1166 if ( ! empty( $excluded_terms ) ) { 1167 if ( ! is_array( $excluded_terms ) ) { 1168 // backcompat, $excluded_terms used to be IDs separated by " and " 1169 if ( false !== strpos( $excluded_terms, ' and ' ) ) { 1170 _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 1171 $excluded_terms = explode( ' and ', $excluded_terms ); 1172 } else { 1173 $excluded_terms = explode( ',', $excluded_terms ); 1162 1174 } 1175 } 1163 1176 1164 1177 $excluded_terms = array_map( 'intval', $excluded_terms ); 1165 1178 1166 if ( ! empty( $term_array ) ) { 1167 $excluded_terms = array_diff( $excluded_terms, $term_array ); 1168 $posts_in_ex_terms_sql = ''; 1169 } 1179 $tax_query[] = array( 1180 'taxonomy' => $tax_query, 1181 'slugs' => $excluded_terms, 1182 'compare' => 'NOT IN', 1183 ); 1184 } 1170 1185 1171 if ( ! empty( $excluded_terms ) ) { 1172 $posts_in_ex_terms_sql = $wpdb>prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy ); 1173 } 1186 // If requesting same term, ensure that excluded terms don't appear in term list list 1187 if ( isset( $terms ) ) { 1188 if ( isset( $excluded_terms ) && is_array( $excluded_terms ) ) { 1189 $terms = array_diff( $terms, $excluded_terms ); 1174 1190 } 1191 1192 if ( ! empty( $terms ) ) { 1193 $tax_query[] = array( 1194 'taxonomy' => $taxonomy, 1195 'terms' => $terms, 1196 ); 1197 } 1175 1198 } 1176 1199 1177 $adjacent = $previous ? 'previous' : 'next'; 1178 $op = $previous ? '<' : '>'; 1179 $order = $previous ? 'DESC' : 'ASC'; 1200 // If we have a tax query, add it to our query args 1201 if ( ! empty( $tax_query ) ) { 1202 $args['tax_query'] = $tax_query; 1203 } 1180 1204 1181 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );1182 $where = apply_filters( "get_{$adjacent}_post_where", $wpdb>prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post>post_type), $in_same_term, $excluded_terms );1183 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );1184 1185 $query = "SELECT p.ID FROM $wpdb>posts AS p $join $where $sort";1186 $query_key = 'adjacent_post_' . md5( $query);1187 $result = wp_cache_get( $query_key, 'counts' );1188 if ( false !== $result ) {1189 if ( $result )1190 $result = get_post( $result );1191 return $result;1205 // And now, the date constraint 1206 if ( $previous ) { 1207 $args['date_query'][] = array( 1208 'before' => $current_post_date, 1209 'inclusive' => true, 1210 ); 1211 } else { 1212 $args['date_query'][] = array( 1213 'after' => $current_post_date, 1214 'inclusive' => true, 1215 ); 1192 1216 } 1193 1217 1194 $result = $wpdb>get_var( $query ); 1195 if ( null === $result ) 1196 $result = ''; 1218 // Ensure the current post isn't returned, since we're using an inclusive date query 1219 $args['post__not_in'] = array( get_the_ID() ); 1197 1220 1198 wp_cache_set( $query_key, $result, 'counts' ); 1221 // Get the posts and return either the post object or null 1222 $args = apply_filters( 'get_adjacent_post_args', $args, compact( 'in_same_term', 'excluded_terms', 'previous', 'taxonony' ) ); 1199 1223 1200 if ( $result ) 1201 $result = get_post( $result ); 1224 $results = get_posts( $args ); 1202 1225 1203 return $result; 1226 if ( is_array( $results ) && ! empty( $results ) ) { 1227 return array_shift( $results ); 1228 } else { 1229 return ''; 1230 } 1204 1231 } 1205 1232 1206 1233 /**