0

I would like to exclude the word "class" from being matched to the search term "lass".

On a site I'm helping manage, the search term "lass" is quite prominent. Unfortunately, that matches the common usage of "class" within the content of many, many WP posts, returning plenty of irrelevant results. However, I still want posts with the word "class" in to be possible matches, I just don't want it to match "lass" to "class".

Here are three possible posts:

1.
<a href="http://path/to/image-large.jpg"><img class="aligncenter size-full wp-image-1234" alt="Image" src="http://path/to/image.jpg"" width="600" height="300" /></a>

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum et veritatis pariatur deleniti excepturi beatae voluptatum aspernatur voluptates rerum numquam.

2.
<a href="http://path/to/image-large.jpg"><img class="aligncenter size-full wp-image-1234" alt="Image" src="http://path/to/image.jpg"" width="600" height="300" /></a>

Lass is lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum et veritatis pariatur deleniti excepturi beatae voluptatum aspernatur voluptates rerum numquam.

3.
Lass is lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum et veritatis pariatur deleniti excepturi beatae voluptatum aspernatur voluptates rerum numquam.

If I was to search for "lass", I would like results 2 and 3 to be returned, but not result 1.

I've seen the excellent results in these two questions, but they didn't quite meet everything I'm looking for. Any suggestions would be appreciated.

1 Answer 1

2

You might try to use the posts_search filter as elegantly suggested by @Kaiser in the first link you provided. Here is one idea:

function filter_search($sql){
    global $wpdb;
    if( strpos(get_query_var("s"),"lass") !== false AND strpos(get_query_var("s"),"class") === false ){
        $sql .= " AND {$wpdb->posts}.post_title NOT LIKE '%class%'";
        $sql .= " AND {$wpdb->posts}.post_content NOT LIKE '%class%'"; 
    }
    return $sql;
}
add_filter( 'posts_search', 'filter_search');

to check if the search term contains lass and not class and then modify the search query to exclude class.

The change in the SQL will look like this:

Before:

AND (((wp_posts.post_title LIKE '%lass%') OR (wp_posts.post_content LIKE '%lass%'))) 

After:

AND (((wp_posts.post_title LIKE '%lass%') OR (wp_posts.post_content LIKE '%lass%'))) 
AND wp_posts.post_title NOT LIKE '%class%' AND wp_posts.post_content NOT LIKE '%class%'

Update:

You can try

   function filter_search($sql){
        global $wpdb;
        if( strpos(get_query_var("s"),"lass") !== false AND strpos(get_query_var("s"),"class") === false ){
            $sql .= " AND ( {$wpdb->posts}.post_content LIKE 'lass %'"; 
            $sql .= " OR {$wpdb->posts}.post_content LIKE '% lass %'"; 
            $sql .= " OR {$wpdb->posts}.post_content LIKE '% lass' )"; 
        }
        return $sql;
    }
    add_filter( 'posts_search', 'filter_search');

if you want to target the exact word lass in the post content.

7
  • s=ahem lass. :P
    – fuxia
    Commented Apr 25, 2013 at 20:24
  • thanks @toscho ;-) I updated the answer, but I'm not sure about the case s=lass+class ;-)
    – birgire
    Commented Apr 25, 2013 at 20:51
  • Thank you for the response, but I'm afraid that excludes any post containing the word "class", which is an awful lot of them. I want it not to match the word "class" when looking for "lass", but still use all posts as potential matches. Commented Apr 26, 2013 at 7:23
  • ok, so you want to exclude 'class %', '% class %' and '% class' instead of '%class%'?
    – birgire
    Commented Apr 26, 2013 at 8:16
  • I'm afraid I'm getting the same results - NOT LIKE is excluding any posts which contain those matches. I'll update my question to make it more clear. Commented Apr 26, 2013 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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