From 86fc3af215a61f042804e3feb57627976d2bb54d Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 18 Oct 2020 23:21:03 +0000 Subject: [PATCH] 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 --- src/wp-includes/canonical.php | 7 ++++++- tests/phpunit/tests/canonical.php | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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 */