Limit WordPress Search Results by Category, Post Type, Post Title

Limit WordPress Search Results by Category, Post Type, Post Title 1

Search is great right? Can you even imagine a day today where you are not using Google search? However, searching was not always this easy.

The most popular search algorithm is the binary search algorithm. Binary search algorithm helps to show faster results because it follows the divide and conquer rule.

What does “divide and conquer” mean? 

It means the search procedure does not go through every result iteratively. In each step, the total searchable data gets divided into half. You can watch the following video to have a quick overview of binary search algorithm.

When Google started back in 1995 at Stanford, they tried to build a search engine by following the same rules as the Nobel Committee. The Nobel committee finds the best scientists by evaluating their contribution to different papers. They followed the same philosophy back then and introduced the concept of PageRank. The more backlinks a page had, the better the rankings they had.

Fast forward to now SEO is important and Google does not rely on only one factor. There are many factors Google considers before giving websites better rankings. One of the main factors is providing users with a great user experience.

Why Search is Important?

Search is crucial to your business’s success. If limiting WordPress search results improve your website’s overall user experience, then you should do it. Because, one of the main factor’s of good SEO rankings is providing great user experience to users. WordPress allows you to exclude categories from search by using plugins.

How Can You Limit WordPress Search Results?

You can limit WordPress search results by using different plugins and codes. We always recommend using plugins to add or modify functionalities because it is a better approach and your customizations still work as before when you update WordPress core.

Best Plugins to Limit WordPress Search Results

There are a few WordPress plugins which work remarkably well for limiting WordPress search results. You can limit WordPress search results by category, by post type, by post tiles and what not.

We are listing some of the best WordPress plugins to limit WordPress search results.

Ivory Search – Limit WordPress Search Results by Category or Post Type

Ivory Search is a simple looking WordPress search plugin, yet adds many advanced features. It enhances the built-in WordPress search and enables you to add new custom forms with specific functions

Why Use Ivory Search for Limiting WordPress Search Results?

  • Configure search forms to search content that you specify.
  • Setup each search forms separately.
  • Limit WordPress search results by category
  • Limit WordPress search results by title
  • Limit WordPress search results by post type
  • Configure WordPress search to include specific categories
  • You can exclude post type from search.

There are a whole lot of customization options you can explore while setting up the pluginIvory Search wordpress search specific category

Relevanssi – A Better Search

Relevanssi has both free and premium version of the plugin. Relevanssi takes a different approach than Ivory search. It replaces the standard WordPress search with its own search engine instead of building on top of it. The plugin is feature packed with many features and configurable options.

You’ll get better results, better presentation of results – your users will thank you because of the improved UX

Why Should You Use Relevanssi to Limit WordPress Search Results?

  • The default search engine is replaced with an improved one.
  • Search results can be sorted in the order of relevance
  • The search engine can match partial words if complete words don’t match.
  • Search posts and pages
  • You can exclude posts, pages, tags or categories from search
  • The premium version adds more features starting from $99/year
Relevanssi Search wordpress search only posts

A word of caution: Relevanssi is not intended for large WordPress installations. If your website has more than 30,000 posts, Relevannsi will not work properly and the site will most probably crash.

Liquid Web

ACF: Better Search

ACF: Better Search is a great plugin that already uses the very popular Advanced Custom Fields plugin. The plugin adds new features to the WordPress search engine. ACF Better Search adds the ability to search by specific content from the selected fields of the Advanced Custom Fields plugin.

There is zero learning curve as everything is configured automatically.

Why Should You Use ACF: Better Search?

  • You can search your custom post types.
  • You can limit search results by custom post type.
wordpress custom post type search

Final Words

There are manual ways you can limit WordPress search results. However, we do not recommend it. There is always a chance to break your site if you write the codes wrong.

If you still want to use code to limit WordPress search results for post titles only, add the following code to your functions.php or add the following code via the Code Snippets plugin.

function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
 
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
 
    $q = $wp_query->query_vars;    
    $n = ! empty( $q['exact'] ) ? '' : '%';
 
    $search =
    $searchand = '';
 
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
 
    if ( ! empty( $search ) ) {
        $search = " AND ({$search}) ";
        if ( ! is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
 
    return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

If you want to limit WordPress search by post type, add the following code to your snippets.

<?php
  // The standard search form
  $form = get_search_form( false );
  
  // Let's add a hidden input field
  $form = str_replace( '<input type="submit"', '<input type="hidden" name="post_type" value="video"><input type="submit"', $form );
  
  // Display our modified form
  echo $form;

We hope you have found this article helpful. Do not forget to check our WordPress SEO Guide to make sure your content is optimized for search results.

KadenceWP
Wordpress Icon

Disclosure: WP Hive earns a commission when you buy through partner links. It does not influence the unbiased opinions of our writers. Learn more →

Share:

https://wphive.com/tutorials/limit-wordpress-search-results/Copy icon

Saif Hassan

Product Manager by passion & profession. Lead Product Manager at weDevs, former PM @ Poptin. Passionate about writing & tech. He's an advocate of Human-Centered Design and believes that websites and the tools used to build them should be well crafted, intuitive, and accessible. Cyclist. Reader. A WordPress ninja 🥷, HCI expert & a design thinker 💡

Subscribe To Our Newsletter

Newsletter Subscription Form

2 Comments

Mohamad Khaled

I have created the plugin Archive Pages in Search that can solve this issue.

The Lite version will redirect the search directly to the archive page whether it is the category or post tag or author.

The Pro version will have an additional option to show the archive page in the search results and add queries related to the archive page. In addition it supports custom taxonomies and custom post type archives.

Link for Lite version:
https://wordpress.org/plugins/archive-pages-in-search-lite/

Link for Pro version:
https://www.codester.com/items/14787/archive-pages-in-search-pro-wordpress-plugin.html?ref=mkhaledche