Skip Navigation

[Resolved] Search returning partial matches

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by Minesh 5 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1243435

I am trying to: Example - when a visitor searches "stress" it should return all service listings containing the word "stress".

Link to a page where the issue can be seen: hidden link

I expected to see: Service listings containing the word "stress" only.

Instead, I got: Unrelated results containing the word "distress"

#1243482

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I would like to know here how you configured the text search? using View or it's a normal WordPress search?

#1243489

Hi there! I'm using Toolset views. Do you know how I can sort this?

Also I would like to learn how visitors can filter results - is there guidance to do help me set this up please?

#1243490

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share the screenshot of your edit view page?

#1243497

Sure, which part?

#1243499

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The whole edit view page that will help 🙂

#1243518
screenshot-mypickle.org-2019.05.14-10-54-50.png

Here you go

#1243523

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you.

Well - to have a exact match of a search keyword, you need to add the following code to your current theme's functions.php file

add_filter( 'posts_search', 'func_search_by_exact_title', 1000, 2 );
function func_search_by_exact_title( $search, $wp_query ){
global $wpdb;
global $WP_Views;
  
if($WP_Views->current_view == 999){
            if ( empty( $search ) )
                    return $search; // skip processing - no search term in query
  
            $q = $wp_query->query_vars;
            $search = '';
            foreach ( (array) $q['search_terms'] as $term ) {
                    $term = esc_sql( like_escape( $term ) );
                    $search = " AND ($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]')";
  
            }
  
}
return $search;
} // Only return an exact match to the input string - not partial matches

Where:
- Replace 999 with your original view ID

#1243526

Is the original view ID the same as the slug? Or is it something different?

Thanks,
Cat

#1243528

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When you will visit Toolset => Views

It will display all views with ID column in tabular format, you need to replace the respective view ID not the slug.