Make WordPress Core


Ignore:
Timestamp:
10/20/2016 06:41:22 PM (9 years ago)
Author:
boonebgorges
Message:

Query: Allow the prefix used for search term exclusion to be filtered.

[38792] allowed WP_Query's hyphen-as-exclusion-prefix feature to be
disabled via filter. A more general solution is to allow the prefix to
be filtered; returning an empty value from a filter callback works to
disable the feature.

Props dlh.
Fixes #38099.

File:
1 edited

Legend:

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

    r38792 r38844  
    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'. This feature
    725      *                                                 can be disabled using the
    726      *                                                 'wp_query_use_hyphen_for_exclusion' filter.
     724     *                                                 return posts containing 'pillow' but not 'sofa'. The
     725     *                                                 character used for exclusion can be modified using the
     726     *                                                 the 'wp_query_search_exclusion_prefix' filter.
    727727     *     @type int          $second                  Second of the minute. Default empty. Accepts numbers 0-60.
    728728     *     @type bool         $sentence                Whether to search by phrase. Default false.
     
    13231323
    13241324        /**
    1325          * Filters whether search terms preceded by hyphens should excluded from results.
     1325         * Filters the prefix that indicates that a search term should be excluded from results.
    13261326         *
    13271327         * @since 4.7.0
    13281328         *
    1329          * @param bool Whether the query should exclude terms preceded with a hyphen.
     1329         * @param string $exclusion_prefix The prefix. Default '-'. Returning
     1330         *                                 an empty value disables exclusions.
    13301331         */
    1331         $hyphen_exclusion = apply_filters( 'wp_query_use_hyphen_for_exclusion', true );
     1332        $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' );
    13321333
    13331334        foreach ( $q['search_terms'] as $term ) {
    1334             // Terms prefixed with '-' should be excluded.
    1335             $include = '-' !== substr( $term, 0, 1 );
    1336             if ( $include || ! $hyphen_exclusion ) {
    1337                 $like_op  = 'LIKE';
    1338                 $andor_op = 'OR';
    1339             } else {
     1335            // If there is an $exclusion_prefix, terms prefixed with it should be excluded.
     1336            $exclude = $exclusion_prefix && ( $exclusion_prefix === substr( $term, 0, 1 ) );
     1337            if ( $exclude ) {
    13401338                $like_op  = 'NOT LIKE';
    13411339                $andor_op = 'AND';
    13421340                $term     = substr( $term, 1 );
    1343             }
    1344 
    1345             if ( $n && $include ) {
     1341            } else {
     1342                $like_op  = 'LIKE';
     1343                $andor_op = 'OR';
     1344            }
     1345
     1346            if ( $n && ! $exclude ) {
    13461347                $like = '%' . $wpdb->esc_like( $term ) . '%';
    13471348                $q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
Note: See TracChangeset for help on using the changeset viewer.