Skip Navigation

[Resolved] When passing a search term into a view I want an exact match

This support ticket is created 5 years, 1 month 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 4 replies, has 3 voices.

Last updated by chrisC-25 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1362991

Based on this view I built into a layout using [wpv-post-title] as a search term:
https://toolset.com/forums/topic/trying-to-use-search-string-filter-in-views/

I want to be able to have it only return exact matches. I run into issues when say a band name is more than one word. For example the band The Record Company is returning posts that have the term record company or record in them. Or another would be The Building that returned results with the word building but I only want "The Building" or "The Record Company". Example here:
hidden link

The top returned result in News should not be there.

Thanks

#1363097

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share the problem URL where you added the view with one example post title and what should be the expected results as well as wp-admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1364387

Minesh, I'm also interested in knowing how to create exact match only searches. For example, if I search "Ana," I only want results if it's "Ana," not "Diana" or "Adriana." Is it possible? Those are post titles by the way.

Sorry for hick-jacking your thread Chris but I think we want the same feature.

#1365315

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

function func_search_by_exact_title( $search, $wp_query ){
global $wpdb;
global $WP_Views;
  
if($WP_Views->current_view == 477){
       
   
  
            if ( empty( $search ) )
                    return $search; // skip processing - no search term in query
  
            		$term = $wp_query->query_vars['s'];
          
                    $search = " AND ($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]')";
   }
return $search;
}
add_filter( 'posts_search', 'func_search_by_exact_title', 1000, 2 );

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

Can you confirm it works as expected now.

#1365557

My issue is resolved now. Thank you!