Skip Navigation

[Resolved] How can I limit Custom Search result to EXACT MATCH of the custom post title?

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I limit Custom Search result to EXACT MATCH of the custom post title?

Solution:
To search by exact title you need to override the query arguments yo u need to use the WordPress 'posts_search' hook.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/how-can-i-limit-custom-search-result-to-exact-match-of-the-custom-post-title/#post-921659

Relevant Documentation:

This support ticket is created 6 years, 5 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 18 replies, has 2 voices.

Last updated by PaulS4783 6 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#921147

I have a page with a search form.
The results get output on a different page.

TWO things aren't happening the way I want.
a) If i leave the field blank and hit SUBMIT, it lists ALL of the custom posts of that type.
It should return an error message ("You must enter a search code")

b) Every post title for the custom post type consists of a unique 10 letter alpha-numeric code.
I want the search to only return EXACT MATCH results.
However, currently, if the user manages to guess two or three letters, the search shows a result for the matching post.

Basically, I ONLY want people who know the exact 10 letter alpha-numeric code of the post title to be able to pull up the results.

#921161

See here:
hidden link

If I type in JFD
I get the result JFD245Y255

What I'd rather see is "no exact match" or similar.

#921479

16 hours and this ticket still not assigned to anybody?

#921600

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - we have unexpected load in forum currently. I'm on it as soon as possible.

#921659

Minesh
Supporter

Languages: English (English )

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

Well - To search by exact title you need to override the query arguments.

could you please try to add following code to your current theme's functions.php file:

function func_search_by_exact_title( $search, $wp_query ){
global $wpdb;
global $WP_Views;

if($WP_Views->current_view == 9999){
			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;
}
add_filter( 'posts_search', 'func_search_by_exact_title', 1000, 2 );

Where:
- Replace 9999 with your original view Id.

#921680

THANK YOU!!
That mostly works....
It now requires a perfect match if I enter something into the text input field...

BUT

If I just hit Search (i.e. with the text input field left blank) it returns a full list of all posts 🙁
I'd like it to generate a (WPML translatable) error message "You must enter a search code".

What's missing?

#921683

Minesh
Supporter

Languages: English (English )

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

To do that - please add following code to your current theme's functions.php file:

add_filter('wpv_filter_query', 'func_exact_title_match', 10, 3);
function func_exact_title_match($query, $setting,$view_id) {
    if($view_id == 9999) {
		
		if(isset($_GET['wpv_post_search']) and empty($_GET['wpv_post_search'])){
			$query['post__in'] = 0;
		}
		
		
    }
return $query;
}

Where:
- Replace 9999 with your original view ID

Now, add following code to your view's filter section - just AFTER this line - [wpv-filter-start hide="false"]:

[wpv-conditional if="( '[wpv-search-term name='wpv_post_search']' eq '' )"]
 [wpml-string context="wpv-views"]You must enter a search code.[/wpml-string]
[/wpv-conditional]

Now, translate the above string:
=> https://toolset.com/documentation/translating-sites-built-with-toolset/translating-content-templates-wordpress-archives-views-cred-forms/
=> https://toolset.com/documentation/translating-sites-built-with-toolset/wpml-string-shortcode/

I hope all will be sorted out and you will achieve exact function you were looking for. Thanks 🙂

#921686

OK. So to be clear, I need to add BOTH filters to the functions.php file?
So the final code looks like this:

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


add_filter('wpv_filter_query', 'func_exact_title_match', 10, 3);
function func_exact_title_match($query, $setting,$view_id) {
    if($view_id == 999) {
         
        if(isset($_GET['wpv_post_search']) and empty($_GET['wpv_post_search'])){
            $query['post__in'] = 0;
        }
            
    }
return $query;
} // If text input field is blank, return nothing.
#921690

Minesh
Supporter

Languages: English (English )

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

Yes - thats correct ? - dont forget to change view ID.

#921692

Thanks. I did. The view ID is 999.

BUT ...

The only problem now is the text "You must enter a search code."
I did as you suggested but it shows up permanently. Even after a successful search.
Full filter code:

[wpv-filter-start hide="false"]
[wpv-conditional if="( '[wpv-search-term name='wpv_post_search']' eq '' )"]
[wpml-string context="wpv-views"]You must enter a search code.[/wpml-string]
[/wpv-conditional]
[wpv-filter-controls]
<div class="form-group">[wpv-filter-search-box placeholder="Enter Shipment Code (10 characters)" output="bootstrap"]</div>
[wpv-filter-submit class="my-button" name="Get Current Shipping Status" output="bootstrap"]
<br><br>
[/wpv-filter-controls]
[wpv-filter-end]
#921695

Minesh
Supporter

Languages: English (English )

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

Uffss... silly mistake.

Please use following code - must work.

[wpv-conditional if="( '[wpv-search-term param='wpv_post_search']' eq '' )"]
 [wpml-string context="wpv-views"]You must enter a search code.[/wpml-string]
[/wpv-conditional]

I hope all sorted - please let me know your feedback 🙂

#921696

Mmmm.
It's a partial success.

Now, when i enter something in the text input field the message doesn't show with the results.

BUT..

The first time the field is displayed, the text is on the screen.
We only want this to appear as an error message AFTER the user forgets to input a code.

#921697

Minesh
Supporter

Languages: English (English )

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

Ahh, you want that way to work. Please use following code:

[wpv-conditional if="( '[wpv-search-term param='wpv_post_search']' eq '' ) AND ( '[wpv-search-term param='wpv_view_count']' ne '' )"]
 [wpml-string context="wpv-views"]You must enter a search code.[/wpml-string]
[/wpv-conditional]

finally? 🙂

#921699

Sadly, no.

So now the error message doesn't appear the first time the form loads - Good.
But it doesn't appear if I submit the form with an empty text input box - Bad.

[wpv-filter-start hide="false"]
[wpv-conditional if="( '[wpv-search-term param='wpv_post_search']' eq '' ) AND ( '[wpv-search-term param='wpv_view_count']' ne '' )"]
 [wpml-string context="wpv-views"]You must enter a search code.[/wpml-string]
[/wpv-conditional]
[wpv-filter-controls]
<div class="form-group">[wpv-filter-search-box placeholder="Enter Shipment Code (10 characters)" output="bootstrap"]</div>
[wpv-filter-submit class="my-button" name="Get Current Shipping Status" output="bootstrap"]
<br><br>
[/wpv-filter-controls]
[wpv-filter-end]
#921701

Minesh
Supporter

Languages: English (English )

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

Well - it works for me. could you please try to refresh page totally and check. It should work.