Title: query_posts codex correct or not?
Last modified: August 19, 2016

---

# query_posts codex correct or not?

 *  [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/)
 * During the course of my delving into WordPress and getting to know it fairly 
   intimately I”ve become confused about the loop due to conflicting information
   around the web and possibly in the codex section on [query_posts](http://codex.wordpress.org/Template_Tags/query_posts).
   
   The codex states
 * > The query_posts function is intended to be used to modify the main page Loop
   > only. It is not intended as a means to create secondary Loops on the page. 
   > If you want to create separate Loops outside of the main one, you should create
   > separate WP_Query objects and use those instead. Use of query_posts on Loops
   > other than the main one can result in your main Loop becoming incorrect and
   > possibly displaying things that you were not expecting.
 * The query_posts function overrides and replaces the main query for the page. 
   To save your sanity, do not use it for any other purpose.
 * Now I’ve visited many sites and purchased a book or two about WordPress and they
   all use the query_posts as a means for creating multiple loops on a page. One
   book recently released by two very prominent WordPress aficionados/gurus, uses
   examples of query_posts as a significant way to include multiple loops on a page.
   If someone could please shed some light on this I’d appreciate it. I have been
   relying very heavily on the codex for reference and example but if it’s not correct
   I’ll have to spend my time with google searches grrrr. Thanks Much.

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/page/2/?output_format=md)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353433)
 * That’s a pretty good article in Codex. So use either get_posts or preferably 
   a new WP_Query for your ‘loops’. For example:
    a simple loop to query posts:
 *     ```
       <?php
           $args=array(
             'post_type' => 'post',
             'post_status' => 'publish',
             'posts_per_page' => -1,
             'caller_get_posts'=> 1
             );
           $my_query = null;
           $my_query = new WP_Query($args);
           if( $my_query->have_posts() ) {
             echo 'List of Posts';
             while ($my_query->have_posts()) : $my_query->the_post(); ?>
             <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
             endwhile;
           }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  Thread Starter [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353446)
 * Okay. One vote for Codex. Regarding your example. What are the objects you have
   populated your array with? I know that “post_type” is a field in the post table
   but what is ‘caller_get_posts’ and ‘posts_per_page’? Can you tell me where I 
   can find an exhaustive list of these objects, so I can get back to work building
   a theme or application. I’m itching to get back to my theme but I hit a wall 
   with the different types of loops and all the confusing info out there. So I 
   feel like I’m back to square one I just want to get this right so when and if
   I release a theme it’s not all mucked up with bad code.
    Thanks
 *  [Rev. Voodoo](https://wordpress.org/support/users/rvoodoo/)
 * (@rvoodoo)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353450)
 * [http://codex.wordpress.org/Function_Reference](http://codex.wordpress.org/Function_Reference)
 * is the list of functions…..
 * [http://codex.wordpress.org/Function_Reference/get_posts](http://codex.wordpress.org/Function_Reference/get_posts)
 * has lots of stuff…..
 *  Thread Starter [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353457)
 * [[
    I just found this link before checking back here. Thanks. On this page it
   states.
 * > This function is used to get a list of all the posts that are defined in the
   > blog. For the most part get_posts can be considered a mini version of query_posts,
   > supporting nearly (but not all) optional parameters. Support parameters are
   > listed below.
 * I went to the [query_post page](http://codex.wordpress.org/Template_Tags/query_posts)
   which also has many parameters listed and usage examples. Can all of this be 
   transfered into the wp_query?
    The [wp_query page](http://codex.wordpress.org/Function_Reference/WP_Query)
   has a list of Methods and Properties but there is little in the way of usage 
   examples. Seems strange if wp_query is the best method for multiple loops that
   there is not more extensive documentation about usage as there is with the other
   two loop methods.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353478)
 * > I went to the query_post page which also has many parameters listed and usage
   > examples. Can all of this be transfered into the wp_query?
 * Yes.
 *  Thread Starter [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353530)
 * Thanks for direction Michael and RVoodoo. A new journey begins.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353566)
 * What purpose does `wp_reset_query()` serve if not to support multiple calls to`
   query_posts()`
 * Comments directly from **wp-includes/query.php** ..
 * > /**
   >  * Destroy the previous query and setup a new query. * * This should be
   > used after {@link query_posts()} and before another {@link * query_posts()}.
   > This will remove obscure bugs that occur when the previous * wp_query object
   > is not destroyed properly before another is setup. * * [@since](https://wordpress.org/support/users/since/)
   > 2.3.0 * @uses $wp_query */
 * As long as `wp_reset_query` is called, it should be valid to use multiple calls
   to `query_posts` should it not?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353580)
 * You have a point there t31os_, but though I thought Otto42’s advice was pretty
   good:
 * “When you want to override the “main” query, you use query_posts.”
 * Note: Otto42 was the one who added that to Codex:
    [http://codex.wordpress.org/index.php?title=Template_Tags%2Fquery_posts&diff=61715&oldid=61371](http://codex.wordpress.org/index.php?title=Template_Tags%2Fquery_posts&diff=61715&oldid=61371)
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353601)
 * Well i can certainly understand that problems may occur as a result of re-using
   the main query. For example if something else on the page relies on looking at
   data in the “main query” (something that loads after your series of loops perhaps),
   it would be looking at incorrect data because the original query has changed (
   you can clone the original query’s data to fix things like this).
 * Overall i agree the advice is good, but i don’t think it’s strictly true that
   you can’t reuse query_posts … i’ve done so without causing any problems as far
   as i can observe, but if i’m making a mistake in doing so i’d be interested in
   a further explanation of the following..
 * > Use of query_posts on other Loops than the main one can result in your main
   > Loop becoming incorrect and possibly displaying things that you were not expecting.
 *  Thread Starter [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353627)
 * t31os_ and MichaelH. I’m glad this conversation has kept going. As many times
   as I’ve seen the query_posts used for multiple loops and if one uses the wp_reset
   query method without any sinister result then I do believe that article in the
   Codex should be revised and revisited. I think using query_posts is the easiest
   way to setup a loop.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353730)
 * I’d say it’s proberly good advice to use a new query, just to avoid causing problems
   with other queries or code that has dependancies on the main query…
 * In short i think the answer is… you can use multiple calls, but don’t be surprised
   if other things sometimes give wrong data sets as a result of re-using the main
   query..
 * I’m not intimately familiar with the WP_Query class, so i’m not sure on it’s 
   limitations, problems or it’s correct (suggested?) usage, so i’ll happily concede
   to anyone who can pick my logic apart… and explain how it should be used, and
   why it should be used a given way…
 *  Thread Starter [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353741)
 * I’d like to hear all about that too. Thanks t31os_
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353772)
 * Adding this wp-hackers thread to the conversation:
    [http://lists.automattic.com/pipermail/wp-hackers/2009-December/029569.html](http://lists.automattic.com/pipermail/wp-hackers/2009-December/029569.html)
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353773)
 * I’m not entirely sure that really addresses the question asked, though i suppose
   it is partially related, i had read the discussion before.
 * Perhaps it would just be easier if i say i agree with Otto in so much that general
   practice should be to create a new query object for each loop … unless you need
   to repeat the same query (you have rewind_posts for that though) .. just to avoid
   those cases when “unpredictable results” can occur ..
 * Again that’s not to say re-using query_posts won’t work, but in general there’s
   far less chance of getting skewed results if a given user chooses to utilize 
   a new query in place of re-using the main query (ie. query posts)..
 * One small question remains though, what was the intention of the reset function
   if not to facilitate the re-use of query_posts?
 * What’s your take Michael, do you generally use a new query object, or do you 
   recycle the main query? … And what was your personal reasoning to using that 
   particular approach over the other? (I’m only curious).
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/#post-1353774)
 * [@t31os_](https://wordpress.org/support/users/t31os_/)
 * Well it just seemed easier to use a new query object and not have to worry about
   messing up the ‘other’ query that WordPress automatically populates. Plus in 
   testing when answering forum questions I’m usually throwing the code at the top
   of index.php or archive.php or sidebar.php for the Default theme and the new 
   query just worked 😉
 * But you are right about the wp_reset_query and I suppose that if you know what
   you are doing, either approach will work.
 * Note – I figured you’d read that hackers thread but just added for future readers.

Viewing 15 replies - 1 through 15 (of 19 total)

1 [2](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/page/2/?output_format=md)

The topic ‘query_posts codex correct or not?’ is closed to new replies.

## Tags

 * [correct](https://wordpress.org/support/topic-tag/correct/)
 * [multiple loops](https://wordpress.org/support/topic-tag/multiple-loops/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 19 replies
 * 5 participants
 * Last reply from: [richarduk](https://wordpress.org/support/users/richarduk/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/query_posts-codex-correct-or-not/page/2/#post-1353793)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
