Opened 8 years ago
Closed 8 years ago
#38099 closed enhancement (fixed)
Ability to disable hyphen-based exclusion in searches
Reported by: | chriseverson | Owned by: | 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)
Change History (25)
#2
@
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
@
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
@
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
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
#7
@
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.
#9
follow-up:
↓ 10
@
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
@
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
#13
@
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 !
#15
@
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
@
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 :)
#17
@
8 years ago
Thanks, @boonebgorges. My attempt is attached as 38099.6.patch.
Hi @chriseverson - Thanks for the ticket!
A filter for this seems like a good idea.