Make WordPress Core


Ignore:
Timestamp:
10/14/2016 08:05:40 PM (8 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r38768 r38792  
    722722     *     @type string       $s                       Search keyword(s). Prepending a term with a hyphen will
    723723     *                                                 exclude posts matching that term. Eg, 'pillow -sofa' will
    724      *                                                 return posts containing 'pillow' but not 'sofa'.
     724     *                                                 return posts containing 'pillow' but not 'sofa'. This feature
     725     *                                                 can be disabled using the
     726     *                                                 'wp_query_use_hyphen_for_exclusion' filter.
    725727     *     @type int          $second                  Second of the minute. Default empty. Accepts numbers 0-60.
    726728     *     @type bool         $sentence                Whether to search by phrase. Default false.
     
    13191321        $searchand = '';
    13201322        $q['search_orderby_title'] = array();
     1323
     1324        /**
     1325         * Filters whether search terms preceded by hyphens should excluded from results.
     1326         *
     1327         * @since 4.7.0
     1328         *
     1329         * @param bool Whether the query should exclude terms preceded with a hyphen.
     1330         */
     1331        $hyphen_exclusion = apply_filters( 'wp_query_use_hyphen_for_exclusion', true );
     1332
    13211333        foreach ( $q['search_terms'] as $term ) {
    13221334            // Terms prefixed with '-' should be excluded.
    13231335            $include = '-' !== substr( $term, 0, 1 );
    1324             if ( $include ) {
     1336            if ( $include || ! $hyphen_exclusion ) {
    13251337                $like_op  = 'LIKE';
    13261338                $andor_op = 'OR';
Note: See TracChangeset for help on using the changeset viewer.