Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use WP_Query's search capability for core post fields #960

Merged
merged 1 commit into from
May 16, 2017

Conversation

jom
Copy link
Member

@jom jom commented May 10, 2017

Closes #894

Changes proposed in this Pull Request:

Addresses issue where search queries would do exact string searches within title and content. WP core breaks up words in a search string and also searches the excerpt. This PR uses this functionality in WP core for the core post fields. Original issue still exists within taxonomy and meta fields.

Testing instructions:

  • Create several published job listings. For example: "Senior UX Designer" and "Senior Web Developer"
  • From the [jobs] shortcode, search for "senior designer" and verify that "Senior UX Designer" appears.
  • Try to also verify the location and job type filters still work.

Proposed changelog entry for your changes:

Enhancement - Improves job filtering by breaking up search terms.

Fixes issue where search queries would do exact string searches within
title and content. WP core breaks up words in a search string and also
searches the excerpt. Original issue still exists within taxonomy and
meta fields.

Fixes #894
@jom jom force-pushed the add/use-wp-query-search branch from 26e5b6a to 85ae79f Compare May 10, 2017 16:27
@Bertsi
Copy link

Bertsi commented May 12, 2017

Hi jom, thanks for your solution. I am testing it right now and theres one problem left:
My title is: professor for computerscience
search: professor for computer (correct match)
search: prof for computerscience (correct match)
search: computerscience prof (correct match)
search: prof for computer (no match)

So the think is, one of the words I am searching for has to be exact, otherwise the search finds nothing. Is there a way finding results without having one exact keyword? For instance: "prof for computer"

Thanks a lot!

@jom
Copy link
Member Author

jom commented May 12, 2017

@Bertsi Looking at WordPress core's code, that should produce results. Mind clearing your transients? I tried it with my environment:
screen shot 2017-05-12 at 10 12 25 am

This is still limited by the restrictions of WordPress core's search capability. The "next level" of search is quite a big jump. In the future, I'm interested in looking at how we can improve compatibility with plugins that augment WordPress' search functionality (like FacetWP and SearchWP, which advertise WPJM compatibility).


return $args;
if ( ! empty( $search ) ) {
$search = preg_replace( '/^ AND /', '', $search );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it is clear what this preg replace is doing. I mean, I can guess, but it would be nice to know what the assumption of what the $search variable has in it and why clearing out the AND keyword works here.

Is the assumption that $search will always have just one clause in it at this point?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WP Query starts its WHERE query segment with 1=1. The posts_search filter prefaces its passed default value with AND. I'm guessing they do this for more predictable chaining, but it still makes using the filter a bit clunky. Other hooked filters may have added other things after the default search query from WP core, but the assumption is that whatever is passed will start with AND or will be an empty string.

Copy link
Contributor

@dbtlr dbtlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I agree with your assessment that the next level of search is a huge jump that is beyond the scope of what core WordPress can really do, without specialized software like elastic search.

It looks good overall, just would like the one point of clarity with the mentioned search clause.

@jom jom requested a review from tripflex May 15, 2017 21:17
@jom
Copy link
Member Author

jom commented May 15, 2017

Holding pending @jchristopher's review for SearchWP. Hoping to get into core next week.

@jchristopher
Copy link

Just took a pass through this PR, all good on my end 👍

Again, thank you so much for the proactive action letting me know about this upcoming change!

@jom jom added this to the 1.26.0 milestone May 16, 2017
@jom jom merged commit 998dcf4 into master May 16, 2017
@jom jom deleted the add/use-wp-query-search branch May 16, 2017 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants