wordpress search exact phrase

wordpress search exact phrase

Rate this article

Share this article

Read wordpress search exact phrase for more information.

WordPress search exact phrase

When we search for a word or a phrase, all kinds of posts related to the keyword are shown up. This is the most commonly asked question about WordPress search. For instance, let’s take a term you might be looking for and search for ‘search exact phrase’. You end up with posts having the terms ‘search’ or ‘phrase’ or ‘exact’. But, if you want to see results with the search term ‘search exact phrase’, it is possible with a twist in the code.

wordpress search exact phrase

This can be achieved by altering the WordPress default query by following different techniques as:

  1.     the pre_get_posts
  2.     the query_posts
  3.     the SQL statement filters
  4.     the wp_the_query

Any of the above techniques can be used for WordPress Search Exact Phrase, but ‘pre_get_posts’ is the easiest way. Just by adding a hook ‘pre_get_posts’ in theme’s functions.php, you can alter the default query. Before the actual query is run, this hook should be called after the query variable object is created. This hook gives the programmer to access the query object by reference.

Code snippet Make code exact

There is a parameter called ‘exact’ which allows setting whether the search query should return results having exactly the searched terms or as set by default. To customize the search to work in exact mode (matching each word from the search terms), you must insert a customized code in your theme’s functions.php file.

Learn more about it by viewing the video:

YouTube video

For all your search needs, alternatively, you can use WP fastest site search plugin from WordPress plugin by Expertrec
  1. Login to your WordPress admin panel.
  2. Click on plugins->add new plugin and search for wp fastest site search . Install the plugin and dont forget to activate it.
    wordpress search not working

Add great search to your wordpress site at 9 USD per month

  1. After activating the plugin you willbe navigated to signup page. It is a two step process. In the step1 you need to enter site url(by default it get filled automatically) and need to select indexing option(if woocommerce is not active).Then click the continue button for step2.wordpress search form
  2. In the step2 you will have 2 options . Either signup through google or you can signup through otp. Choose an option and complete signup.Wordpress search not working
  3. Make sure you have upgraded to a paid plan before 15 days of free trial. (Price begins at 9 USD per month)
  4. For a detailed explaination regarding our plugin integration, features and how it will help in your search you can refer this blog  Wordpress fastest site search
  5. That’s it! Your brand new WordPress search is up and running! Here are snapshots of the search form and the search results pages.
  6. Snapshot of your WordPress search autocomplete.wordpress search not working
  7. Snapshot of your WordPress search results page.wordpress search not working
  8. Snapshot of your WordPress search spelling error tolerance feature.wordpress search not working
  9. If the plugin method didn’t work, you can add the code from https://cse.expertrec.com/csedashboard/home/code and add it to your wordpress PHP code. Please note that you have to add separately the code <ci-search></ci-search> to a place where you want the search box to appear ( Tip!- Contact our support team to get this done). There might be a one-time fee for integration.
    <script>
    (function() {
    var id = 'your_api_key';
    var ci_search = document.createElement('script');
    ci_search.type = 'text/javascript';
    ci_search.async = true;
    ci_search.src = 'https://cse.expertrec.com/api/js/ci_common.js?id=' + id;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ci_search, s);
    })();
    </script>
    <ci-search></ci-search>

    wordpress search not working

Add great search to your wordpress site at 9 USD per month

YouTube video

 

When it comes to improving the search functionality on your WordPress site, achieving precise and accurate results is paramount. WordPress, by default, provides a basic search mechanism, but there are various techniques you can employ to enhance and customize it for exact match results. In this blog post, we will explore four powerful methods: pre_get_posts, query_posts, SQL statement filters, and wp_the_query.

Utilizing pre_get_posts:

The pre_get_posts action hook is a powerful tool for modifying the default query before it is executed. By hooking into this action, you can alter parameters such as the search query, post type, and other essential parameters. This allows you to fine-tune the search process and prioritize exact matches.

Example code snippet:

function custom_search_exact_match($query) {

 if ($query->is_search) {

 $query->set('exact', true);

 }

}

add_action('pre_get_posts', 'custom_search_exact_match');

In this example, we set the exact parameter to true, ensuring that the search query performs an exact match.

Implementing query_posts:

The query_posts function is another way to customize the WordPress query. It allows you to override the main query but should be used cautiously to avoid unintended consequences. When used judiciously, it can be a handy tool for refining the search results.

Example code snippet:

function custom_search_exact_match() {

 if (is_search()) {

 query_posts(

 array(

 's' => get_search_query(),

 'exact' => true,

 )

 );

 }

}

add_action('template_redirect', 'custom_search_exact_match');

Here, we use query_posts to modify the search query and enforce an exact match.

Leveraging SQL statement filters:

For advanced users comfortable with SQL, modifying the SQL query directly can provide granular control over the search process. The posts_search filter enables you to customize the SQL WHERE clause of the search query.

Example code snippet:

function custom_search_exact_match($search, $query) {

 global $wpdb;

 if ($query->is_search) {

 $search = preg_replace("/( {$wpdb->posts}.post_title LIKE '%)(%s%)(')/", "$1{$wpdb->posts}.post_title = '$2'$3", $search);

 }

 return $search;

}

add_filter('posts_search', 'custom_search_exact_match', 10, 2);

This code adjusts the SQL query to enforce an exact match on the post title during searches.

Enhancing wp_the_query:

The wp_the_query filter provides a way to modify the main query just before it is executed. By using this filter, you can apply adjustments specifically tailored to your search requirements.

Example code snippet:

function custom_search_exact_match($query) {

 if ($query->is_search) {

 $query->set('exact', true);

 }

}

add_filter('wp_the_query', 'custom_search_exact_match');

Similar to pre_get_posts, this code ensures that the search query performs an exact match.

Conclusion:

Customizing WordPress search for exact match results involves exploring different techniques, each offering a unique approach to refining the default search functionality. Whether using action hooks like pre_get_posts and template_redirect, SQL statement filters, or the wp_the_query filter, the key is to strike a balance between customization and maintaining the integrity of your site’s functionality. Experiment with these techniques, test thoroughly and implement the solutions that best suit your specific needs.

Add great search to your wordpress site at 9 USD per month

Are you showing the right products, to the right shoppers, at the right time? Contact us to know more.
You may also like