Disable the Search Feature in WordPress



Sometimes the search feature becomes unnecessary, when you are using WordPress as a CMS. If you want to get rid of the search functionality of WordPress, here is the solution you can use. In this post I will show you how to disable the search feature in WordPress.

Open your theme’s functions.php file and paste below code:

function disable_search_filter_query( $query, $error = true ) {

	if ( is_search() ) {
		unset( $_GET['s'] );
		unset( $_POST['s'] );
		unset( $_REQUEST['s'] );
		
		$query->query_vars[s] = false;
		$query->query[s] = false;
		$query->is_search = false;
		$query->set( 's', '' );

		// set to error
		if ( $error == true )
			$query->is_404 = true;
	}
}
add_action( 'parse_query', 'disable_search_filter_query' );

function disable_search_form($form){
	return '';
}
add_filter( 'get_search_form', 'disable_search_form', 10 );

This code will disable the search feature in WordPress and whenever the user tries for the search he will be redirected to your theme’s 404 page, so you must have the 404 template file in your theme. But if you have set the $error value to false, user will stay on the page where they have tried to run the search.