Search by Hyphen

One approach is to modify the exclusion prefix through the wp_query_search_exclusion_prefix filter that’s supported in WP 4.7+.
See ticket #38099.

Here’s an example how we can change it from - to e.g. !:

add_filter( 'wp_query_search_exclusion_prefix', function( $prefix )
{
    return '!'; // adjust to your needs (default is -)
} );

where we would use !apple instead of -apple to exclude apple from the search results. Then you can search by -.

It looks like the exclusion prefix must be a single character (or empty to disable this feature), because of this check in the core:

// If there is an $exclusion_prefix, terms prefixed with it should be excluded.
$exclude = $exclusion_prefix && ( $exclusion_prefix === substr( $term, 0, 1 ) );
if ( $exclude ) {
    $like_op  = 'NOT LIKE';
    $andor_op = 'AND';
    $term     = substr( $term, 1 );
} else {
    $like_op  = 'LIKE';
    $andor_op = 'OR';
}

Otherwise it sounds like a bug, not to be able to search only for the term exclusion prefix.

I wonder if it would be better to add an extra $exclusion_prefix !== $term check to support that:

$exclude = $exclusion_prefix 
    && $exclusion_prefix !== $term 
    && ( $exclusion_prefix === mb_substr( $term, 0, 1 ) );
if ( $exclude ) {
    $like_op  = 'NOT LIKE';
    $andor_op = 'AND';
    $term     = mb_substr( $term, 1 );
} else {
    $like_op  = 'LIKE';
    $andor_op = 'OR';
}

where we would also use mb_substr() instead of substr() for a wider char support for the exclusion prefix.

I guess I should just create ticket for this …

Leave a Comment