1

I have search over the google, somewhere out there and here(http://wordpress.stackexchange.com). But, i can't seem to get the solution i was looking for.

I would like to ask that, is there any solution which allows me to exclude the words i wanted to filter from search queries? Example: when visitors using my search box, they type "email" and search, the result returns to no post found with this keyword.. All the post contains the word of "email". But, i don't want visitors to get the search result by searching email. I want them to get the best related result.

I found this question(Exclude Specific Term from Search), i think that this question is similar to what i am asking here. But, i don't understand how it works.

I wanted to exclude a few words that hiding from search result so that visitors won't get result with non related post. Any solution? Thanks!

  • I posted an answer but reading through your question again, I'm not sure what you're after so I hope I'm not misunderstanding it ;-) – birgire Apr 3 '15 at 9:16
  • Hi @birgire, you got what i mean. I commented to your answer for less than 5 minutes time. – Jornes Apr 3 '15 at 9:29
3

If I understand you correctly you're not looking for the wp_search_stopwords filter type of solution.

So here's a modification of the great answer by @kaiser that you referred to:

/**
 * Exclude array of words from all search queries in the front-end
 *
 * Modification of http://wordpress.stackexchange.com/a/41100/26350 by @kaiser
 */

add_filter( 'posts_search', function( $search, $q )
{
    // Target all search queries in the front-end:
    if( is_admin() || ! $q->is_search() ) return $search;

    global $wpdb;

    $exclude_words = [ 'foo', 'bar' ]; // <-- Modify this to your needs!

    $sql = " AND {$wpdb->posts}.post_title   NOT LIKE '%%%s%%' 
             AND {$wpdb->posts}.post_content NOT LIKE '%%%s%%' ";

    foreach( (array) $exclude_words as $word )
        $search .= $wpdb->prepare(
           $sql,
           $wpdb->esc_like( $word ),
           $wpdb->esc_like( $word )
        );

    return $search;
}, 10, 2 );

where you have to modify the $exclude_words to your needs. Here we target all search queries in the front-end.

Notice the '%%%s%%' part. It will be transformed into '%someword%' in the generated SQL query. It's also tempting to use %1$s for repeating use of the same string, but it's not supported by $wpdb->prepare(). If we would use sprintf instead, we have to make sure it's not translated into the $s variable within a string with double quotes: "$s".

If we only want to modify the main search query, we could use:

    if( is_admin() || ! $q->is_main_query() || ! $q->is_search() ) return $search;

instead of this check:

    if( is_admin() || ! $q->is_search() ) return $search;

Update:

We can now search with foo -bar to exclude bar from the foo search.

Then one could play with something like:

add_action( 'pre_get_posts', function( \WP_Query $q )
{
    if ( ! is_admin() && $q->is_main_query() && $q->is_search() )
        $q->set( 's', $q->get( 's' ) . ' -bar' );

} );

Note that the search exclusion prefix can be changed with the wp_query_search_exclusion_prefix filter.

  • Hi! @birgire, I tried your code and add the words that i want to filter to the $exclude_words. It works fine with the search result. But, it leads some of my post returning to Page Not Found. Do you have any idea on this issue? – Jornes Apr 3 '15 at 9:26
  • So you get a 404 for no search results found? Does that happen without this code snippet? @Jornes – birgire Apr 3 '15 at 9:34
  • Hi @birgire, nope! The search result was fine. All the posts contained the words i have filtered are not turning up when using the search box. But, i get 404 error page for when i visit all the posts which contained the words i have filtered using the code you provided. Do you know how to fix it up? Thanks! – Jornes Apr 3 '15 at 9:55
  • ok, please try the updated answer @Jornes where I target all search queries. You might want to restrict it to the main query search instead, by using is_search(). – birgire Apr 3 '15 at 10:31
  • HI @birgire, awesome! It works now. Thank you so much! By the way, what was the issue in previous code? – Jornes Apr 3 '15 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.