Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 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

1 Answer 1

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):

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

    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.

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

    $query->init();

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

    // re-run validation and conditional set up.
    $query->parse_query();
}
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

 
discard

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.