Canonical: Support multiple post types in `redirect_guess_404_permalink()`.

Prevent `redirect_guess_404_permalink()` from throwing a notice when multiple post types are included in the `post_type` query.

Props junaidbhura.
Fixes #43056.



git-svn-id: https://develop.svn.wordpress.org/trunk@49200 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2020-10-18 23:21:03 +00:00
parent 0392a498db
commit 86fc3af215
2 changed files with 26 additions and 1 deletions

View File

@ -895,7 +895,12 @@ function redirect_guess_404_permalink() {
// If any of post_type, year, monthnum, or day are set, use them to refine the query.
if ( get_query_var( 'post_type' ) ) {
$where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
if ( is_array( get_query_var( 'post_type' ) ) ) {
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
$where .= " AND post_type IN ('" . join( "', '", esc_sql( get_query_var( 'post_type' ) ) ) . "')";
} else {
$where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
}
} else {
$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
}

View File

@ -275,6 +275,26 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase {
$this->assertFalse( redirect_guess_404_permalink() );
}
/**
* Ensure multiple post types do not throw a notice.
*
* @ticket 43056
*/
public function test_redirect_guess_404_permalink_post_types() {
/*
* Sample-page is intentionally missspelt as sample-pag to ensure
* the 404 post permalink guessing runs.
*
* Please do not correct the apparent typo.
*/
// String format post type.
$this->assertCanonical( '/?name=sample-pag&post_type=page', '/sample-page/' );
// Array formatted post type or types.
$this->assertCanonical( '/?name=sample-pag&post_type[]=page', '/sample-page/' );
$this->assertCanonical( '/?name=sample-pag&post_type[]=page&post_type[]=post', '/sample-page/' );
}
/**
* @ticket 43745
*/