WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It looks like an e-commerce plugin's core query will be of no use to me in one particular template so I would like to discard it completely in favor of my own WP_Query loops in the template. It's not the main query, but a secondary loop query_posts() call that I'm trying to discard downstream.

wp remove query has some good no-op pure SQL suggestions, but what would be a recommended way to stick one of those into that specific $query in pre_get_posts?

share|improve this question
How is the query being excuted? If it is in an action hook, you may be able to remove it using remove_action()‌​. – Joseph Leedy Jan 26 '13 at 3:29
Probably not, there's a bunch of other necessary stuff going on in that action still. None of the least selecting the template etc. – lkraav Jan 26 '13 at 8:27

There's not really a way to "stop" the main query. There are plenty of filters to modify it, just nothing to turn it off completely. The best you may be able to do is hook into posts_request, which fires right before the data is hit, and return an empty string or something non-sensical. This is still going to hit the database.

Example (untested):

add_filter('posts_request', 'wpse83187_posts_request', 10, 2);
function wpse83187_posts_request($sql, $query)
    if (!$query->is_main_query() /* other conditionals here! */) {

    return '';

Instead of creating your own WP_Query looks in the template, why not just completely reset the query with pre_get_posts?

Then use the "main" loop for at least one of your WP_Query loops. No database hits happen until much later, so the only "wasted" overhead is WP filling query vars. WP_Query has a method called init that basically resets everything. Call it, then set whatever query variables you need. Then you'll need to call parse_query again to set up the various conditionals.

add_action('pre_get_posts', 'wpse83187_reset_q');
function wpse83187_reset_q($query)
    if (!$query->is_main_query() /* other conditionals here! */) {


    // reset query vars here. eg:
    $query->set('post_type', 'product');

    // re-run validation and conditional set up.
share|improve this answer
Thanks. Of course I would gladly do part of the work in pre_get_posts, but it's going to be the standard "group by taxonomy" archive page problem. I think it doesn't make much sense to split this query up into two places. As a matter of fact, it's not the main query I'm trying to stop, at all. It's a secondary query_posts() call. But I guess the problem remains the same at heart. – lkraav Jan 26 '13 at 8:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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