wecobble / Subtitles

Add subtitles into your WordPress posts, pages, custom post types, and themes. No coding required. Simply activate Subtitles and you're ready to go.
http://wordpress.org/plugins/subtitles/
GNU General Public License v2.0
117 stars 186 forks source link

Include subtitles in search results; #37 #49

Open bcole808 opened 9 years ago

bcole808 commented 9 years ago

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:

  4. 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.
  5. 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 commented 9 years ago

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.