diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 6a74ac2d43..167b5cf562 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -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 ) ) ) . "')"; } diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php index 895e5bb44e..bd0b68c038 100644 --- a/tests/phpunit/tests/canonical.php +++ b/tests/phpunit/tests/canonical.php @@ -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 */