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.

Possible Duplicate:
When to use WP_query(), query_posts() and pre_get_posts

I just noticed today that the documentation for query_posts() mentions some "disadvantages" of using query_posts for altering the main Loop, notably: 'causes additional SQL queries'.

This seems to imply that there's another way / a better way. Obviously there's get_posts() and WP_Query for secondary loops, but I don't see them as addressing the "disadvantages" listed in the Codex documentation.

I can see that by waiting until you're in the template to run query_posts, WordPress has already run a query once and this is now a second query that clobbers the first one (the first one being basically ignored). This definitely DOES sound inefficient (though perhaps not a big deal, who knows?)

My question is: is there an alternative to query_posts that does NOT add "additional SQL queries" or is the Codex documentation simply deceiving?

share|improve this question

marked as duplicate by Rarst Jul 29 '12 at 12:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 16 down vote accepted

When I added the drawbacks to the Codex, I was mainly thinking of using the 'request' filter as an alternative to query_posts().

That filter is only run for the main query, so that solves the problem with 'pre_get_posts', which fires for every query.

The downside is that you don't have access to query flags like is_single() etc.

Here's one way you could get access to them, without actually doing the SQL queries:

function alter_the_query( $request ) {
    $dummy_query = new WP_Query();  // the query isn't run if we don't pass any query vars
    $dummy_query->parse_query( $request );

    // this is the actual manipulation; do whatever you need here
    if ( $dummy_query->is_home() )
        $request['category_name'] = 'news';

    return $request;
}
add_filter( 'request', 'alter_the_query' );
share|improve this answer
    
Thanks scribu, this is exactly what I was hoping to learn, and straight from the source, too! I'd like to update that Codex entry at some point since it seems a little scattered. I wonder whether it makes sense to include this code snippet you just included as well? Rarst's cautions are also well advised! –  Tom Auger Jun 30 '11 at 19:53
    
It wouldn't hurt. –  scribu Jun 30 '11 at 19:56
1  
Done: codex.wordpress.org/Plugin_API/Filter_Reference/request and major cleanup codex.wordpress.org/Function_Reference/query_posts. I would appreciate if you might check to make sure I haven't made any blunders. –  Tom Auger Jul 4 '11 at 17:17
    
Looks pretty good. –  scribu Jul 5 '11 at 11:22
1  
Is there a reference available regarding how to alter the $request object? For instance, how can I redirect a Page to index.php and only list a specific category there using this method? I imagine I have to unset() the pagename key, but what else is there? –  Isaac Lubow Jul 28 '11 at 0:55

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