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

Include subtitles in search results; #37 #49

Open
wants to merge 2 commits into
base: master
from

Conversation

Projects
None yet
2 participants
@bcole808

bcole808 commented Jun 23, 2015

Hello -- I stumbled upon your plugin and fit the need for our website perfectly. The code is well organized and makes good use of filters and hooks to allow the plugin behavior to be further adjusted.

One thing we needed was the ability for the search function to also search the subtitle field (#37).

I tried a lot of different ways to achieve this; this pull request is what I finally came up with that achieved the desired behavior. However, I am not 100% certain that this implementation will not cause conflicts when other plugins or themes may attempt to further filter the search query, so it would be good if someone else can also look over this and do some testing to see if this implementation works safely.

My solution

The solution in this pull request uses the posts_clauses filter to directly modify the MySQL query clauses.

It performs regex matching to find the search term and create an additional WHERE clause.

The pros are:

  1. It keeps the query safe because WordPress has already sanitized the search term before placing it in the query clauses.
  2. It retains the default WordPress behavior for multiple search terms -- in other words, when the user searches for "foo bar" it will be broken into two separate terms, "foo" and "bar" and this method retains that specificity in the query.
  3. It works in the Admin Dashboard and front-end

The cons are:

  1. It directly modifies the MySQL clauses and may have unintended side-effects if other themes or plugins also attempt to filter the search queries. Further testing is required.
  2. It does not work if Meta Query parameters have been set by another plugin; in this case, the Meta Query works and the subtitle field is simply not searched.

Alternate method which did not work

I tried one other way to achieve this feature: Using the WP_Query Meta Query functionality would have been preferable because it would have made use of the built-in WP_Query object instead of modifying SQL directly. For example:

public function search_subtitles( $query ) {

    if ( $query->is_search ) {
        $args = array(
            array(
                'key'     => '_subtitle',
                'value'   => '%user_search_term%',
                'compare' => 'LIKE'
            ),
        );
        $query->set( 'meta_query', $args ); 
    }

    return $query;
}

This unfortunately does not work because in a search query, the results are still filtered down to only posts which have the user_search_term present in either the post_title or post_content fields.

From searching online, others were able to implement this type of functionality by performing two queries and combining the results: 1) to search only the title and content fields, and 2) to search only the meta fields.

@philiparthurmoore

This comment has been minimized.

Member

philiparthurmoore commented Jun 23, 2015

Ben, thank you very much for this Pull Request. I will give it a look soonest and give you feedback as soon as I've had a chance to dig into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment