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

Viewing 4 replies - 16 through 19 (of 19 total)
  • Thread Starter tsalagi

    (@tsalagi)

    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

    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.
    https://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.

    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.

    richarduk

    (@richarduk)

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

    https://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;?>

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘query_posts codex correct or not?’ is closed to new replies.