How to disable WordPress’s internal search system (while still using its search page templates)

On websites like Sportsnet.ca ( http://www.sportsnet.ca/ ) and Maclean’s ( http://www.macleans.ca/ ), the amount of content on those WordPress websites long ago exceeded the level that the built-in WordPress search system can capably handle.

Third-party search solutions such as Google CSE (Custom Search Engine) are being utilized instead, but there is a momentary slowdown before the page loads that is caused by the WordPress site executing its own internal search query before it displays the search results page. In order to remove this delay (and the unnecessary database request), add the following code to the theme’s functions.php file:

[code]
// Disable WordPress’s internal search query (as much as we can), letting Google CSE handle that.
function internal_search_disable( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( $query->is_search ) {
// Add a filter that effectively returns no results ever.
add_filter(‘posts_where’, ‘internal_search_filter_where’);
}
}
return $query;
}
add_action(‘pre_get_posts’, ‘internal_search_disable’);

// The WHERE search filter for disabling the internal search system.
function internal_search_filter_where( $where = ” ) {
$where = " AND 0 = 1";

// Once added, remove the filter to stop affecting other queries on the page.
remove_filter(‘posts_where’, ‘internal_search_filter_where’);
return $where;
}
[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *