Forums

query_posts codex correct or not? (20 posts)

  1. Tsalagi
    Member
    Posted 3 years ago #

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

  2. MichaelH
    Member
    Posted 3 years ago #

    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().
    ?>
  3. Tsalagi
    Member
    Posted 3 years ago #

    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

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

  5. Tsalagi
    Member
    Posted 3 years ago #


    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 which also has many parameters listed and usage examples. Can all of this be transfered into the wp_query?
    The wp_query page 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.

  6. MichaelH
    Member
    Posted 3 years ago #

    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.

  7. Tsalagi
    Member
    Posted 3 years ago #

    Thanks for direction Michael and RVoodoo. A new journey begins.

  8. Mark / t31os
    Moderator
    Posted 3 years ago #

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

  9. MichaelH
    Member
    Posted 3 years ago #

    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

  10. Mark / t31os
    Moderator
    Posted 3 years ago #

    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.

  11. Tsalagi
    Member
    Posted 3 years ago #

    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.

  12. Mark / t31os
    Moderator
    Posted 3 years ago #

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

  13. Tsalagi
    Member
    Posted 3 years ago #

    I'd like to hear all about that too. Thanks t31os_

  14. MichaelH
    Member
    Posted 3 years ago #

    Adding this wp-hackers thread to the conversation:
    http://lists.automattic.com/pipermail/wp-hackers/2009-December/029569.html

  15. Mark / t31os
    Moderator
    Posted 3 years ago #

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

  16. MichaelH
    Member
    Posted 3 years ago #

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

  17. Tsalagi
    Member
    Posted 3 years ago #

    Michael. I read the short post in the link you provided. I didn't see any reset used in either example. Here is an excellent post about this subject that some may find useful.
    Undocumented WordPress Query Function: wp_reset_query()
    I agree that using wp_reset_query is the most effecient way to get things done allowing the multiple use of query_posts. I'm assuming that is why the wp_reset_query was created in the first place.
    Changing the codex may clear up some of the confusion and misconceptions. query_posts. Removing

    Important note
    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.

    , and replacing it with correct usage and the requiring the inclusion of wp_reset_query.

    There still seems to be some confusion about the use of wp_reset_query with $WP_Query. Does every instance of query_posts and $WP_Query need to be reset? Should the reset be used at the beginning of every new custom loop? For the sake of clarity, I think these issues need to be addressed before adding specific usage instruction to the wp_reset_query page in the codex.

    Cheers

  18. Mark / t31os
    Moderator
    Posted 3 years ago #

    Codex is maintained by several people, of which may have very different ideas of code usage, or justifications for why they do things one way over another..

    If you feel that strongly about the information, drop an email onto the codex mailing list, where your request will get the attention it requires.
    http://lists.automattic.com/mailman/listinfo/wp-docs

    Alternatively submit a revised version (would need to read codex rules/guidelines for doing that) and submit it to the codex team for approval.

    What's ideally needed here, is just some input from the user(s) who initially implemented or wrote said function(s), so their usage can be explained definitively, rather then us just making assumptions on what usage could or should be, because i'll be honest... i don't really know what's correct or not... i'm just making an educated guess here.

  19. MichaelH
    Member
    Posted 3 years ago #

    Tsalagi - as far as I understand it, wp_reset_query should be put at the end of any custom query that uses the_post() so as to reset the values back to what the 'main query' might use.

  20. richarduk
    Member
    Posted 3 years ago #

    What do people think of this? Based on code from here:

    http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/

    In particular look at the first two lines and the last line.

    Is there any need to use <?php wp_reset_query();?>? Should I add that after the last line as an extra safety measure?

    <?php
    $temp = $second_loop;
    $second_loop= null;
    $second_loop = new WP_Query();
    $second_loop->query('posts_per_page=1&cat=8,7'.'&paged='.$paged);
    ?>
    
    <?php if ($second_loop->have_posts()) :?>
    <?php  while ($second_loop->have_posts()) : $second_loop->the_post(); ?>   
    
    <?php endwhile; ?>  
    
    <?php endif; ?>
    
    <?php else : ?>        
    
    <h2 class="no-posts">Sorry! </h2>
    
    <?php endif; ?>    
    
    <?php $second_loop = null; $second_loop = $temp;?>

Topic Closed

This topic has been closed to new replies.

About this Topic