Site icon MonstersPost

Useful WordPress Code Snippets to Improve Search

WordPress, the versatile CMS powering over one-fifth of the entire web has only a few weak points to talk about. Its built-in search is one such feature that limits its capabilities to a great extent.

The search is too vague to be of much use and leaves most users baffled. Would not it be wonderful if you had a tool or custom code to bolster up its search function so that your site visitors get a great experience every single time they visit your site and optimally use the search function of your site! Here are some useful WordPress code snippets to do that.

1. Exclude Pages or Posts

In case you want certain pages or posts to be excluded from the results page, you can copy and paste this code in the functions.php file. This code excludes categories with ids 0 and 1 from search results. Specify the appropriate categories to suit your needs.

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('cat','0,1');
}
  return $query;
}
add_filter('pre_get_posts','SearchFilter');

2. If there is only one post, redirect to it by default

If there is only one post, you would want your visitors to be directed to it by default. Use the following code to achieve this purpose:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

3. Search Form with Drop Down having categories

It would be great if you could limit your search to a specific category. The following code will help in adding a drop down form with all the categories.

">
  
in

4. Show the number of Results

While performing search on a WordPress blog, the number of results obtained is not shown on the search page. The following code:

Search Results

in the search.php file needs to be replaced with this code:

Search Results for post_count; _e(''); _e(''); echo $key; _e(''); _e(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?>

5. Specify a particular post type for searching

One can search specific post types using a simple code snippet that can be added to the functions.php file:

function SearchFilter($query) {
  if ($query->is_search) {
    // Insert the specific post type you want to search
    $query->set('post_type', 'feeds');
  }
  return $query;
}

// This filter will jump into the loop and arrange our results before they're returned

add_filter('pre_get_posts','SearchFilter');

6. Enlight searched text

Searched text can be enlightened using a simple code snippet. In the search.php file, replace the_title() function with echo $title;

Now, add this snippet just before the code you just added:

\0',
		$title);
?>

7. Use jQuery to highlight search terms

The following code snippet can be used for highlighting search terms to help users.

function hls_set_query() {
  $query  = attribute_escape(get_search_query());

  if(strlen($query) > 0){
    echo '
      
    ';
  }
}
function hls_init_jquery() {
  wp_enqueue_script('jquery');
}

add_action('init', 'hls_init_jquery');
add_action('wp_print_scripts', 'hls_set_query');
And then add this code into your header.php file, just before the /head tag:

  

8. Limit the number of posts per page

By default, WordPress displays 10 posts per page. If you wish to change this number, you can add the following code to your functions.php file:

function limit_posts_per_search_page() {
	if ( is_search() )
		set_query_var('posts_per_archive_page', 20); 

The number in line number three can be changed to any value suitable to you.

9. Make the WordPress search unlimited

WordPress happens to display up to 10 posts by default. You can make your WordPress search unlimited by replacing the following code in search.php by the snipped provided below.
Replace:

 

With this:


10. Completely shut down WordPress Search

It is even possible to completely shut down WordPress search. Copy this code into your functions.php file to achieve this purpose:

function fb_filter_query( $query, $error = true ) {
    if ( is_search() ) {
        $query->is_search = false;
        $query->query_vars[s] = false;
        $query->query[s] = false;
 
        // to error
        if ( $error == true )
            $query->is_404 = true;
    }
}
 
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );