Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#38099 closed enhancement (fixed)

Ability to disable hyphen-based exclusion in searches

Reported by: chriseverson's profile chriseverson Owned by: dlh's profile dlh
Milestone: 4.7 Priority: normal
Severity: normal Version: 4.4
Component: Query Keywords: has-unit-tests has-patch
Focuses: Cc:

Description

In 4.4.0, the ability to prepend a search term with hyphen to indicate exclusion was added to the codebase.

This behavior is awesome 99.9% of the time, however I recently found myself working on site where this behavior is problematic due to the fact that their products are named, quite often, using hyphens, in the same fashion.

https://sunrisescience.com/?s=PABA&post_type=product is an example of this need in action. Searching PABA returns all products with "PABA" in the title, however, the project requirements in this case require the ability to search for "-PABA" in the title and not "PABA" without the preceding hyphen.

I'd propose a new filter to toggle this functionality on and off, and will send a patch for that as soon as I find some time over the next day or two.

Attachments (6)

38099.patch (884 bytes) - added by chriseverson 8 years ago.
Patch for this issue using a new filter "wp_search_exclusion" with a default of true
38099.2.patch (897 bytes) - added by chriseverson 8 years ago.
Updated patch filter name to "wp_query_use_hyphen_for_negation". Unit tests coming soon.
38099.3.patch (3.2 KB) - added by choongsavvii 8 years ago.
38099.4.patch (3.2 KB) - added by choongsavvii 8 years ago.
38099.5.patch (3.2 KB) - added by choongsavvii 8 years ago.
38099.6.patch (4.3 KB) - added by dlh 8 years ago.

Download all attachments as: .zip

Change History (25)

#1 @boonebgorges
8 years ago

  • Keywords needs-patch added
  • Version changed from 4.6.1 to 4.4

Hi @chriseverson - Thanks for the ticket!

A filter for this seems like a good idea.

@chriseverson
8 years ago

Patch for this issue using a new filter "wp_search_exclusion" with a default of true

#2 @chriseverson
8 years ago

  • Keywords needs-patch removed

For testing: In the above patch, it expects the following to disable the hyphen being a search operator:

add_filter( 'wp_search_exclusion', '__return_false' );

I'm not entirely in love with the naming of the filter, so if anyone has any better ideas, I can update accordingly.

#3 @boonebgorges
8 years ago

  • Keywords needs-unit-tests needs-refresh added
  • Milestone changed from Awaiting Review to 4.7
  • Owner set to chriseverson
  • Status changed from new to assigned

Patch looks good, @chriseverson!

How about something more verbose for the filter name? Maybe wp_query_use_hyphen_for_negation?

If you're able to write a test that demonstrates that the filter works, it'd help to expedite this ticket. The existing tests are here https://core.trac.wordpress.org/browser/tags/4.6.1/tests/phpunit/tests/query/search.php#L62

@chriseverson
8 years ago

Updated patch filter name to "wp_query_use_hyphen_for_negation". Unit tests coming soon.

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


8 years ago

#5 @chriscct7
8 years ago

  • Keywords needs-refresh removed

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

#7 @choongsavvii
8 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

Greetings from WordCamp NL contributor day!

38099.3.patch contains @chriseverson 's original patch, to which I added unit tests. Also updated the doc comment for the $s parameter, to make it easier to find this new filter.

#8 @choongsavvii
8 years ago

  • Keywords has-patch added

#9 follow-up: @choongsavvii
8 years ago

Apologies - one little update. Something didn't feel right. The filter was named _negation, but the variable and comments were all talking about _exclusion. I think the correct naming should be wp_query_use_hyphen_for_exclusion . The word 'negation' is used in a boolean context, but that is not the case here - we are actually excluding stuff.

38099.4.patch reflects this renaming.

#10 in reply to: ↑ 9 @chriseverson
8 years ago

I agree, "exclusion" seems much more semantically appropriate than "negation".

This ticket was mentioned in Slack in #core by choong. View the logs.


8 years ago

#12 @boonebgorges
8 years ago

  • Owner changed from chriseverson to boonebgorges

I'll review this.

#13 @choongsavvii
8 years ago

One more adjustment, as suggested by @jorbin in Slack:

`__return_true` is for calbacks with things like `add_filter`. You don't pass a callback to `apply_filters`, you pass the value you want to filter. In this case we want to filter the bool

Changed in 38099.5.patch - thanks @jorbin !

#14 @boonebgorges
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 38792:

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.

#15 @dlh
8 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

I apologize for reopening this ticket after the earlier work on it and so late in this cycle's enhancement window.

It seems to me like core could allow a developer working with content containing semantically important hyphens to still support search exclusions by letting them change the "exclusion prefix" to something other than a hyphen.

Would folks be amenable to a patch that allowed the prefix to be filtered, rather than a filter that toggles the whole feature? If so, I'm happy to help try to write it. The filter could also support "return false to disable" for developers who need it.

Or, let me know if I'm misunderstanding something.

#16 @boonebgorges
8 years ago

  • Owner changed from boonebgorges to dlh
  • Status changed from reopened to assigned

@dlh Sure, I'd be glad to see a patch that does that. The sooner the better :)

@dlh
8 years ago

#17 @dlh
8 years ago

Thanks, @boonebgorges. My attempt is attached as 38099.6.patch.

#18 @boonebgorges
8 years ago

Thank you, @dlh! This looks like an improvement. I'm going to make a few changes for readability and go with it.

#19 @boonebgorges
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 38844:

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.

Note: See TracTickets for help on using tickets.