How to Disable the Search Feature in WordPress

Posted on May 15th, 2010 by in Tutorials | 5 Comments  
How to Disable the Search Feature in WordPress

When using WordPress as a CMS, sometimes the search feature becomes unnecessary. Well if you have been wanting to get rid of the search functionality, then this article is just for you. In this article, we will show you how to disable the search feature in WordPress.

Open your theme’s functions.php file and add the following codes:

    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;" ) );

Note: if you set $error to be true, then the user will be redirected to your 404 page (theme’s 404.php must exist). If you set it as false, then the user stays at the page where they tried to run the search.

Source: WPEngineer

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

Heh! Thanks for the tips I was looking to do this at one moment and had no clue on how I can do it!

I'm having a hard time understanding the usefulness of this code. If search is unnecessary wouldn't it make more sense to not include the search form? If you leave a search form on a page and then don't let someone use that search form isn't that very poor usability?

Am I missing something?

You can remove the search form in the design, but the functionality stays. Anyone who knows it is WordPress can simply add a /?s=keyword and search the site. But by removing the form and adding this code will take care of that problem. There will be time when you have a client that only wants pages or posts to be shown if the URL is available. This function will come handy in that sense.

Hmmmm.... can't imaging Search not being important for a CMS. Isn't the fact that WordPress has such a fine Search feature a huge advantage when using it as a CMS? I can imaging Search only being not important on a small portfolio site say limited to 5 pages or so.

There are times when you are using WordPress to create a standalone site for a client where you only want people to know about the pages if they have the URL.

Tweets about us: