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

Limit your WordPress Search Results by Category, Post Type or Post Title to offer users a better experience.

Last Updated on

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 rule as the Nobel committee. The Nobel committee finds the best scientist 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 2019, 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 in 2019, 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.

How to Manage WordPress Automatic Updates Like a Pro! [2019]

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

Ivory Search – WordPress Search Plugin

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.

Relevanssi – A Better Search

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.

ACF: Better Search

ACF: Better Search is a great plugin who already uses the very popular Advanced Custom Fields plugin. The plugin adds new features to 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.

ACF: Better Search

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.

Writing What You Want to Read – 5 Audience Engagement Strategies to Increase User Engagement

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.

Leave a Reply