From 9f05bf8ed9be54f6036eef60a40860bd9e427677 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 14 Mar 2016 14:11:16 +0000 Subject: [PATCH] Query: Ignore search terms consisting of a single dash. Due to the "exclude" support added in WP 4.4, single dashes were being converted to "NOT LIKE '%%'" clauses, causing all searches to fail. Props RomSocial, swissspidy. Fixes #36195. git-svn-id: https://develop.svn.wordpress.org/trunk@36989 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 4 ++-- tests/phpunit/tests/query/search.php | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 4b3e1e41f7..7bebfb4932 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2194,8 +2194,8 @@ class WP_Query { else $term = trim( $term, "\"' " ); - // Avoid single A-Z. - if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) ) + // Avoid single A-Z and single dashes. + if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) ) continue; if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) ) diff --git a/tests/phpunit/tests/query/search.php b/tests/phpunit/tests/query/search.php index 35f734b9c0..7f7db1986f 100644 --- a/tests/phpunit/tests/query/search.php +++ b/tests/phpunit/tests/query/search.php @@ -126,6 +126,31 @@ class Tests_Query_Search extends WP_UnitTestCase { $this->assertEqualSets( array( $p3 ), $q->posts ); } + /** + * @ticket 36195 + */ + public function test_s_should_not_exclude_for_dashes_between_words() { + $p1 = self::factory()->post->create( array( + 'post_status' => 'publish', + 'post_content' => 'This post has foo but also bar', + ) ); + $p2 = self::factory()->post->create( array( + 'post_status' => 'publish', + 'post_content' => 'This post has only bar', + ) ); + $p3 = self::factory()->post->create( array( + 'post_status' => 'publish', + 'post_content' => 'This post has only foo - bar', + ) ); + + $q = new WP_Query( array( + 's' => 'foo - bar', + 'fields' => 'ids', + ) ); + + $this->assertEqualSets( array( $p1, $p3 ), $q->posts ); + } + /** * @ticket 35361 */