diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 8d6830ca5f..c603eddbd5 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1513,7 +1513,7 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo } if ( ! empty( $excluded_terms ) ) { - $where .= " AND p.ID NOT IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . implode( $excluded_terms, ',' ) . ') )'; + $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( $excluded_terms, ',' ) . ') )'; } } diff --git a/tests/phpunit/tests/link.php b/tests/phpunit/tests/link.php index 88816faa14..9e3a39d1a0 100644 --- a/tests/phpunit/tests/link.php +++ b/tests/phpunit/tests/link.php @@ -190,10 +190,17 @@ class Tests_Link extends WP_UnitTestCase { } /** - * @ticket 22112 - */ + * @ticket 22112 + */ function test_get_adjacent_post_exclude_self_term() { - $include = $this->factory->category->create(); + // Bump term_taxonomy to mimic shared term offsets. + global $wpdb; + $wpdb->insert( $wpdb->term_taxonomy, array( 'taxonomy' => 'foo', 'term_id' => 12345 ) ); + + $include = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'name' => 'Include', + ) ); $exclude = $this->factory->category->create(); $one = $this->factory->post->create_and_get( array(