From 24bf60bb4feffed532eef5e0839930981d0a4f28 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 4 Sep 2013 18:50:04 +0000 Subject: [PATCH] Kill the query in the following edge case: `post_type => 'any'` but `exclude_from_search => false` returns no valid post types. Adds unit tests. Props mitchoyoshitaka. Fixes #19198. git-svn-id: https://develop.svn.wordpress.org/trunk@25239 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 4 +++- tests/phpunit/tests/query/results.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index af558fe06e..7dfaae037e 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2455,7 +2455,9 @@ class WP_Query { if ( 'any' == $post_type ) { $in_search_post_types = get_post_types( array('exclude_from_search' => false) ); - if ( ! empty( $in_search_post_types ) ) + if ( empty( $in_search_post_types ) ) + $where .= ' AND 1=0 '; + else $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')"; } elseif ( !empty( $post_type ) && is_array( $post_type ) ) { $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')"; diff --git a/tests/phpunit/tests/query/results.php b/tests/phpunit/tests/query/results.php index 34ca0db201..4486209466 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -369,4 +369,23 @@ class Tests_Query_Results extends WP_UnitTestCase { 'child-two', ), wp_list_pluck( $posts, 'post_title' ) ); } + + function test_exlude_from_search_empty() { + global $wp_post_types; + foreach ( array_keys( $wp_post_types ) as $slug ) + $wp_post_types[$slug]->exclude_from_search = true; + + $posts = $this->q->query( array( 'post_type' => 'any' ) ); + + $this->assertEmpty( $posts ); + $this->assertRegExp( '#AND 1=0#', $this->q->request ); + + foreach ( array_keys( $wp_post_types ) as $slug ) + $wp_post_types[$slug]->exclude_from_search = false; + + $posts2 = $this->q->query( array( 'post_type' => 'any' ) ); + + $this->assertNotEmpty( $posts2 ); + $this->assertNotRegExp( '#AND 1=0#', $this->q->request ); + } }