Query: Allow the hyphen-prefix-for-search-exclusion feature to be disabled by filter.

WordPress 4.4 introduced "hyphen exclusion" for search terms, so that
"foo -bar" would return posts containing "foo" AND not containing "bar".
The new filter 'wp_query_use_hyphen_for_exclusion' allows developers
to disable this feature when it's known that their content will contain
semantically important leading hyphens.

Props chriseverson, choongsavvii.
Fixes #38099.
Built from https://develop.svn.wordpress.org/trunk@38792


git-svn-id: http://core.svn.wordpress.org/trunk@38735 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2016-10-14 20:06:28 +00:00
parent 8cbb988196
commit 9783a3df6b
2 changed files with 15 additions and 3 deletions

View File

@ -721,7 +721,9 @@ class WP_Query {
* @type array $post_name__in An array of post slugs that results must match.
* @type string $s Search keyword(s). Prepending a term with a hyphen will
* exclude posts matching that term. Eg, 'pillow -sofa' will
* return posts containing 'pillow' but not 'sofa'.
* return posts containing 'pillow' but not 'sofa'. This feature
* can be disabled using the
* 'wp_query_use_hyphen_for_exclusion' filter.
* @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
* @type bool $sentence Whether to search by phrase. Default false.
* @type bool $suppress_filters Whether to suppress filters. Default false.
@ -1318,10 +1320,20 @@ class WP_Query {
$n = ! empty( $q['exact'] ) ? '' : '%';
$searchand = '';
$q['search_orderby_title'] = array();
/**
* Filters whether search terms preceded by hyphens should excluded from results.
*
* @since 4.7.0
*
* @param bool Whether the query should exclude terms preceded with a hyphen.
*/
$hyphen_exclusion = apply_filters( 'wp_query_use_hyphen_for_exclusion', true );
foreach ( $q['search_terms'] as $term ) {
// Terms prefixed with '-' should be excluded.
$include = '-' !== substr( $term, 0, 1 );
if ( $include ) {
if ( $include || ! $hyphen_exclusion ) {
$like_op = 'LIKE';
$andor_op = 'OR';
} else {

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38791';
$wp_version = '4.7-alpha-38792';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.