Skip Navigation

[Resolved] Keyword search needs to be absolute

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

Last updated by erikE-3 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1118903
elos-search.png

This search gives two results because the keyword (ref-no) is similar to another.
hidden link

The first result on that page has ref.no PMA-ATE36-1 and because of that it is also visible in the search results. Eventhoug we don't want to show it. It should only appear if you search for PMA-ATE36-1.

We've added those REF. Numbers in the content area of each page and also as taxonomies.
I guess WP only search in title and body but how can we make the search to be exact. and not show similar posts?

#1118992

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - I checked the problem URL you shared but I do not see all the filters which displayed with the screenshot you shared. I can only see the "Choose your region:" filter.

Could you please tell me which field you would like to work as the exact match - and you want to apply the exact match against post title and post body search?

If possible, please share access details, so I can check how you setup your filters.

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#1119024

Hi Minesh,

If you visit this page and first select Denmark as region:
hidden link
Then add the REF number in the "Product number REF#"-field (se previous screenshot for correct )

So basically I want to make this field to search for the exact phrase used in that field:

[wpml-string context="wpv-views"]Product Number - REF:[/wpml-string]<br /> [wpv-filter-search-box]

The code for the view I'm using is this:

[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="row">
<div class="eifu-field col-md-6"> [wpml-string context="wpv-views"]Choose your region:[/wpml-string]<br /> [wpv-control taxonomy="eifu-region" url_param="wpv-eifu-region" type="select" default_label="Select Region" taxonomy_order="ASC" taxonomy_orderby="name" hide_empty="true"]</div>
<div id="extended" style="visibility: hidden;" class="eifu-field col-md-6">[wpml-string context="wpv-views"]Product Number - REF:[/wpml-string]<br /> [wpv-filter-search-box]</div></div>
<div class="row">
<div id="extended1" style="visibility: hidden; clear: both;" class="eifu-field col-md-6">[wpml-string context="wpv-views"]Product Group:[/wpml-string] <br />[wpv-control taxonomy="eifu-productgroup" url_param="wpv-eifu-productgroup" type="select" default_label="Select Product Group" taxonomy_order="ASC" taxonomy_orderby="name" hide_empty="true"]</div>
<div id="extended2" style="visibility: hidden;" class="eifu-field col-md-6">[wpml-string context="wpv-views"]Product:<br />[/wpml-string] [wpv-control taxonomy="eifu-productmodel" url_param="wpv-eifu-productmodel" type="select" default_label="Select Product" hide_empty="true"]</div>
</div>
<div id="extended3" style="visibility: hidden; clear: both;">[wpv-filter-reset reset_label="Reset Filter" class="btn btn-primary" type="input"]</div>
[wpv-filter-spinner container="div" position="before" spinner="<em><u>hidden link</u></em>"][/wpv-filter-spinner]
[/wpv-filter-controls]
[wpv-filter-end]
#1119025

Hi Minesh,

If you visit this page and first select Denmark as region:
hidden link
Then add the REF number (please, see previous screenshot for correct ref# to use) in the "Product number REF#"-field

So basically I want to make this field to search for the exact phrase used in that field:

[wpml-string context="wpv-views"]Product Number - REF:[/wpml-string]<br /> [wpv-filter-search-box]

The code for the view I'm using is this:

[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="row">
<div class="eifu-field col-md-6"> [wpml-string context="wpv-views"]Choose your region:[/wpml-string]<br /> [wpv-control taxonomy="eifu-region" url_param="wpv-eifu-region" type="select" default_label="Select Region" taxonomy_order="ASC" taxonomy_orderby="name" hide_empty="true"]</div>
<div id="extended" style="visibility: hidden;" class="eifu-field col-md-6">[wpml-string context="wpv-views"]Product Number - REF:[/wpml-string]<br /> [wpv-filter-search-box]</div></div>
<div class="row">
<div id="extended1" style="visibility: hidden; clear: both;" class="eifu-field col-md-6">[wpml-string context="wpv-views"]Product Group:[/wpml-string] <br />[wpv-control taxonomy="eifu-productgroup" url_param="wpv-eifu-productgroup" type="select" default_label="Select Product Group" taxonomy_order="ASC" taxonomy_orderby="name" hide_empty="true"]</div>
<div id="extended2" style="visibility: hidden;" class="eifu-field col-md-6">[wpml-string context="wpv-views"]Product:<br />[/wpml-string] [wpv-control taxonomy="eifu-productmodel" url_param="wpv-eifu-productmodel" type="select" default_label="Select Product" hide_empty="true"]</div>
</div>
<div id="extended3" style="visibility: hidden; clear: both;">[wpv-filter-reset reset_label="Reset Filter" class="btn btn-primary" type="input"]</div>
[wpv-filter-spinner container="div" position="before" spinner="<em><u>hidden link</u></em>"][/wpv-filter-spinner]
[/wpv-filter-controls]
[wpv-filter-end]
#1119036

Minesh
Supporter

Languages: English (English )

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

Well - to find the exact match, you need to modify the view's query on the fly.

Please 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.

#1120490
refno.png

Hi again Minesh.

I've added the code to my functions.php now and also changed view id to the correct one.

However, It seems that this code doesn't solve my problem since it's focused on the content of the title, not the body.

The REF# is not visible in the title, It's part of the body
Please see attached image for explanation of the setup for this particular CPT.

Thanks, Pierre

#1120623

Minesh
Supporter

Languages: English (English )

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

Well - there is no such feature but please check following link and adjust your search query as required.
=> hidden link
=> hidden link

#1120712

Well, actually I solved it pretty easy.
Since I have the ref numbers in the post_content area all I had to do was to change your code from referring to posts.post_title to posts.post_content and then everything work as it should 🙂

#1120713

My issue is resolved now. Thank you!