Skip to content

Commit 4ebab4d

Browse files
committed
Remove extraneous table join in get_adjacent_post().
Since [29248], a table join has not been necessary to process the `$excluded_terms` parameter of `get_adjacent_post()`. Aside from adding extra overhead, this join meant that post records that don't have any corresponding rows in `wp_term_relationships` were erroneously excluded from results. Fixes #32833. git-svn-id: https://develop.svn.wordpress.org/trunk@34088 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ed36c88 commit 4ebab4d

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

src/wp-includes/link-template.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,9 +1492,6 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
14921492
$where = '';
14931493

14941494
if ( $in_same_term || ! empty( $excluded_terms ) ) {
1495-
$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";
1496-
$where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1497-
14981495
if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
14991496
// back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
15001497
if ( false !== strpos( $excluded_terms, ' and ' ) ) {
@@ -1508,6 +1505,9 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
15081505
}
15091506

15101507
if ( $in_same_term ) {
1508+
$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";
1509+
$where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
1510+
15111511
if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
15121512
return '';
15131513
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

tests/phpunit/tests/link.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,73 @@ function test_get_adjacent_post_exclude_self_term() {
257257
$this->assertEmpty( get_adjacent_post( false, array(), false ) );
258258
}
259259

260+
/**
261+
* @ticket 32833
262+
*/
263+
public function test_get_adjacent_post_excluded_terms() {
264+
register_taxonomy( 'wptests_tax', 'post' );
265+
266+
$t = $this->factory->term->create( array(
267+
'taxonomy' => 'wptests_tax',
268+
) );
269+
270+
$p1 = $this->factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) );
271+
$p2 = $this->factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) );
272+
$p3 = $this->factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) );
273+
274+
wp_set_post_terms( $p2, array( $t ), 'wptests_tax' );
275+
276+
// Fake current page.
277+
$_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
278+
$GLOBALS['post'] = get_post( $p1 );
279+
280+
$found = get_adjacent_post( false, array( $t ), true, 'wptests_tax' );
281+
282+
if ( ! is_null( $_post ) ) {
283+
$GLOBALS['post'] = $_post;
284+
} else {
285+
unset( $GLOBALS['post'] );
286+
}
287+
288+
// Should skip $p2, which belongs to $t.
289+
$this->assertEquals( $p3, $found->ID );
290+
}
291+
292+
/**
293+
* @ticket 32833
294+
*/
295+
public function test_get_adjacent_post_excluded_terms_should_not_require_posts_to_have_terms_in_any_taxonomy() {
296+
register_taxonomy( 'wptests_tax', 'post' );
297+
298+
$t = $this->factory->term->create( array(
299+
'taxonomy' => 'wptests_tax',
300+
) );
301+
302+
$p1 = $this->factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) );
303+
$p2 = $this->factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) );
304+
$p3 = $this->factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) );
305+
306+
wp_set_post_terms( $p2, array( $t ), 'wptests_tax' );
307+
308+
// Make sure that $p3 doesn't have the 'Uncategorized' category.
309+
wp_delete_object_term_relationships( $p3, 'category' );
310+
311+
// Fake current page.
312+
$_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
313+
$GLOBALS['post'] = get_post( $p1 );
314+
315+
$found = get_adjacent_post( false, array( $t ), true, 'wptests_tax' );
316+
317+
if ( ! is_null( $_post ) ) {
318+
$GLOBALS['post'] = $_post;
319+
} else {
320+
unset( $GLOBALS['post'] );
321+
}
322+
323+
// Should skip $p2, which belongs to $t.
324+
$this->assertEquals( $p3, $found->ID );
325+
}
326+
260327
public function test_wp_make_link_relative_with_http_scheme() {
261328
$link = 'http://example.com/this-is-a-test-http-url/';
262329
$relative_link = wp_make_link_relative( $link );

0 commit comments

Comments
 (0)