0

I'm using a custom Google search engine to display results on my site, rather than WordPress's built-in search functionality. I've done this by modifying our theme's search.php template to replace the standard loop with the code from Google. Everything is working swimmingly.

However, WordPress is still wasting time/energy performing the default search of the database, even though the results will never be used. Can I somehow prevent WordPress from even performing this default search?

  • do you want no query at all to run? the problem is that template functions expect a WP_Query object to exist. To make this work with nothing whatsoever being sent to the database for the main query, you need to fill that object with valid data or you'll get all sorts of errors. – Milo Nov 11 '14 at 19:52
  • Since I don't care about the results of any query, that'd probably be ideal, but as you said - I think that would ultimately cause more issues. Also, I'm probably exaggerating the cost of the search query. By the time I jump through a ton of hoops to try and prevent the query from happening, I could probably have just ran it and ignored the results! – MadtownLems Nov 11 '14 at 19:55
  • I'm currently thinking that the best bet is to force set 'no_found_rows' to be true. While part of the query will still run, it seems to be a nice and easy way to help performance here. – MadtownLems Nov 11 '14 at 19:56
  • Milo - That looks very much like what I was originally trying to accomplish, but also supports the idea that doing so is incredibly hacky. :/ Thanks a lot for sharing! – MadtownLems Nov 24 '14 at 20:46
0

Many tutorials exist for disabling the search - see http://wpzine.com/disable-wordpress-search/

Basically it boils down to unsetting the query vars - here is the gist of it...

function disable_search( $query, $error = true ) {
  if ( is_search() ) {
  $query->is_search = false;
  $query->query_vars[s] = false;
  $query->query[s] = false;
  if ( $error == true )
    $query->is_404 = true;
  }
}
add_action( 'parse_query', 'disable_search' );

That said, not sure how if any this might impact your google search implementation.

  • Yeah, this snippet is all over the web. It definitely has significantly greater implications than just halting execution of the "select matching posts" query. Thanks, though. – MadtownLems Nov 11 '14 at 19:53
  • this doesn't prevent a query from being run, it just converts search queries to the default posts query that would run on your posts page. – Milo Nov 11 '14 at 19:55
0

You should be able to do this via the pre_get_posts filter, which is run before the actual query is performed.

add_filter( 'pre_get_posts', 'wpse168169' );
function wpse168169( $query ) {
  if ( ( ! is_admin() ) && ( $query->is_main_query() ) && ( $query->is_search ) ) {
    // modify $query here to suit your needs; unset( $query ) maybe?
  }
  return $query;
}
  • Yeah, the 'modify $query here' part is where I don't know what to do. I've tried unset($query), but the search query is still performed. :| – MadtownLems Nov 11 '14 at 19:40
  • try @rfair404's answer; different filter, but same idea - and he's provided the 'modify here' part – karpstrucking Nov 11 '14 at 19:52
  • I believe that parse_query is a much better time than pre_get_posts, and rather than mucking with lots of the query attributes, you can just return false! – MadtownLems Nov 24 '14 at 20:47
1

Same issue with you and just found it out on stackexchange. Put codes below in the function.php in your theme.

function _cancel_query( $query ) {
    if ( !is_admin() && !is_feed() && is_search() ) {
        $query = false;
    }
    return $query;
}
add_action( 'posts_request', '_cancel_query' );

Here's the source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.