[wp-trac] [WordPress Trac] #39483: Use $wp_the_query for the main loop and pagination

WordPress Trac noreply at wordpress.org
Thu Jan 5 15:12:28 UTC 2017


#39483: Use $wp_the_query for the main loop and pagination
-------------------------+------------------------------
 Reporter:  prosti       |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Query        |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by prosti):

 `query_posts()` and altering the main loop? Some believe that this is not
 so good idea.
 Instead they advice `pre_get_posts` action hook to do that. (WPSO)

 I see `query_posts()` as a nice function that outputs directly to the
 global `wp_query` variable for the arguments given and this is it.

 ----

 When one mention the main loop they usually think of this:


 {{{
 while ( have_posts() ) : the_post();
 endwhile;
 }}}

 At least the 2010-2017-teen default themes all used that.
 But we have the freedom.

 2018-teen may have the loop based on `$wp_the_query` and

 {{{
 while ( wp_have_posts() ) : wp_the_post();
 endwhile;
 }}}

 // ps. just for the example I set some function names, `wp_the_post` we
 could write also `$wp_the_query->the_post()` instead

 And after that loop consider in a template we need to show 10 posts from
 category `trattatattrat` so we run

 {{{
 query_posts( $arts );
 while ( have_posts() ) : the_post();
 endwhile;
 }}}

 So several loops in a single template. This is OK.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39483#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list