diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index ae01e6fe0c..dd04b24731 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1492,9 +1492,6 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo $where = ''; if ( $in_same_term || ! empty( $excluded_terms ) ) { - $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"; - $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); - if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and " if ( false !== strpos( $excluded_terms, ' and ' ) ) { @@ -1508,6 +1505,9 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo } if ( $in_same_term ) { + $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"; + $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); + if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) return ''; $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); diff --git a/tests/phpunit/tests/link.php b/tests/phpunit/tests/link.php index 1090efe6a5..ec41155481 100644 --- a/tests/phpunit/tests/link.php +++ b/tests/phpunit/tests/link.php @@ -257,6 +257,73 @@ class Tests_Link extends WP_UnitTestCase { $this->assertEmpty( get_adjacent_post( false, array(), false ) ); } + /** + * @ticket 32833 + */ + public function test_get_adjacent_post_excluded_terms() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + ) ); + + $p1 = $this->factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) ); + $p2 = $this->factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) ); + $p3 = $this->factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) ); + + wp_set_post_terms( $p2, array( $t ), 'wptests_tax' ); + + // Fake current page. + $_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; + $GLOBALS['post'] = get_post( $p1 ); + + $found = get_adjacent_post( false, array( $t ), true, 'wptests_tax' ); + + if ( ! is_null( $_post ) ) { + $GLOBALS['post'] = $_post; + } else { + unset( $GLOBALS['post'] ); + } + + // Should skip $p2, which belongs to $t. + $this->assertEquals( $p3, $found->ID ); + } + + /** + * @ticket 32833 + */ + public function test_get_adjacent_post_excluded_terms_should_not_require_posts_to_have_terms_in_any_taxonomy() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + ) ); + + $p1 = $this->factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) ); + $p2 = $this->factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) ); + $p3 = $this->factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) ); + + wp_set_post_terms( $p2, array( $t ), 'wptests_tax' ); + + // Make sure that $p3 doesn't have the 'Uncategorized' category. + wp_delete_object_term_relationships( $p3, 'category' ); + + // Fake current page. + $_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; + $GLOBALS['post'] = get_post( $p1 ); + + $found = get_adjacent_post( false, array( $t ), true, 'wptests_tax' ); + + if ( ! is_null( $_post ) ) { + $GLOBALS['post'] = $_post; + } else { + unset( $GLOBALS['post'] ); + } + + // Should skip $p2, which belongs to $t. + $this->assertEquals( $p3, $found->ID ); + } + public function test_wp_make_link_relative_with_http_scheme() { $link = 'http://example.com/this-is-a-test-http-url/'; $relative_link = wp_make_link_relative( $link );