Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canonical: Validate NOT EXISTS tax_query does not error #3530

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/wp-includes/canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$term_count = 0;

foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
$term_count += count( $tax_query['terms'] );
if ( isset( $tax_query['terms'] ) && is_countable( $tax_query['terms'] ) ) {
$term_count += count( $tax_query['terms'] );
}
}

$obj = $wp_query->get_queried_object();
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/tests/canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,31 @@ public function test_utf8_query_keys_canonical() {

delete_option( 'page_on_front' );
}

/**
* Ensure NOT EXISTS queries do not trigger not-countable or undefined array key errors.
*
* @ticket 55955
*/
public function test_feed_canonical_with_not_exists_query() {
// Set a NOT EXISTS tax_query on the global query.
$global_query = $GLOBALS['wp_query'];
$GLOBALS['wp_query'] = new WP_Query(
array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'operator' => 'NOT EXISTS',
),
),
)
);

$url = redirect_canonical( get_term_feed_link( self::$terms['/category/parent/'] ), false );
$this->assertNull( $url );

// Restore original global.
$GLOBALS['wp_query'] = $global_query;
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
}
}