1

I would like to disable the MySQL portion of the main query, i.e. I want every step to be followed on this page EXCEPT step 4.3: http://codex.wordpress.org/Query_Overview

What's the easiest way to go about doing that?

2
  • 2
    Do you want to force query to have no results or to completely disable query being performed at all? Latter might be... complicated. :)
    – Rarst
    Commented May 4, 2013 at 20:01
  • 1
    Essentially I want to minimize the burden of that first query, so if returning no results accomplishes that, that would be acceptable.
    – Guillochon
    Commented May 4, 2013 at 20:02

2 Answers 2

2

Check if this solution work for your case:

add_filter('posts_request', 'supress_main_query', 10, 2);
function supress_main_query( $request, $query ){
    if( $query->is_main_query() && ! $query->is_admin )
        return false;
    else
        return $request;
}

posts_request is the last filter called before running the query, and pass to you the $requestvariable with the generated SQL string and $query, with the WP_query object used to generate the query.

This way you can check if this is the main query, and return false to don't run the generated query.

4
  • It causes just about everything to 404, which is exactly what I'd expect if that query doesn't run.
    – s_ha_dum
    Commented May 4, 2013 at 23:03
  • Exactly. But 404 only happens on single pages, not archives or home, maybe it's sufficient to the users need. As the user wants to "minimize the burden", some filters to optimize the query would be better, but I can say for sure without knowing the context :) Commented May 4, 2013 at 23:55
  • 1
    Single posts 404. Pages 404. Date archives 404. Author, tag, and category archives return 200 but show nothing found. I don't think anything is wrong with your answer. I'm just puzzled by this request in general. Like I said, this is what I'd expect if the code works to disable that query.
    – s_ha_dum
    Commented May 5, 2013 at 0:30
  • 1
    @s_ha_dum Perfect case for this is when routing custom rewrite rules with custom query vars that access data from a custom table, by nullifying the main query, then filtering through post clauses provided by WP_Query/get_posts I've been able to get WP_Query & template tags to work on custom tables as if they were post types in wp_posts table which means I get all the benefits of template hierarchies and helper functions, took some work. So the request is definitely a valid one because there exists numerous use-cases to warrant in doing so. Saving on unnecessary queries is always a good thing.
    – Adam
    Commented Aug 2, 2014 at 11:13
1

It typically hard to throw out completely thing that WP insist on doing during core load process. It is however relatively easy to mess with them in smaller ways and sufficient results.

Query has a lot of filters, namely posts_where (with is_main_query() check to be sure we are only modifying it) allows us to change WHERE part of SQL query.

But what do we add to it to get rid of it? Typically following condition is used AND 1=0. Since it's very obviously not satisfiable it efficiently short circuits SQL query into no results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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