WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#39483 new enhancement

Use $wp_the_query for the main loop and pagination

Reported by: prosti Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Query Keywords:
Focuses: Cc:
PR Number:

Description

query_posts() hasn't been deprecated. And it should not.
http://wordpress.stackexchange.com/questions/226960/why-query-posts-isnt-marked-as-deprecated

Nothing bad with that function.

However, we may improve things further.

The main query will set both the globals $wp_the_query and $wp_query.

https://i.stack.imgur.com/nsaZ5.png

The loop =

while ( have_posts() ) : the_post();
...
endwhile;

and pagination that works via paginate_links()

are both dependent on $wp_query or =f($wp_query)

However, the frozen variable is $wp_the_query.

This is why I suggest the while loop needs to be organized to depend on $wp_the_query instead. The very same should be the case for the pagination function paginate_links().


This way we may use query_posts even without the need to run wp_reset_query(), since query_posts breaks these two variables ( $wp_query and $wp_the_query ) being equal.

https://i.stack.imgur.com/RWSbJ.png


Change History (4)

#1 @SergeyBiryukov
3 years ago

This way we may use query_posts even without the need to run wp_reset_query()

The very purpose of query_posts() is to alter the main loop. If it no longer affects the loop as per your suggestion, what would it be used for?

#2 follow-up: @prosti
3 years ago

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 considers in a template we need to show 10 posts from category tattarrattat so we run another loop, not the main one.

query_posts( $args );
while ( have_posts() ) : the_post();
endwhile;

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

Last edited 3 years ago by prosti (previous) (diff)

#3 in reply to: ↑ 2 @SergeyBiryukov
3 years ago

Replying to 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)

That's correct.

Custom loops are already possible using new WP_Query(), I don't see the need to change query_posts() for that.

#4 @prosti
3 years ago

Custom loops are already possible using new WP_Query(), I don't see the need to change query_posts() for that.

I haven't proposed to change the query_posts.
The possible enhancement means more freedom for query_posts, and less problems with breaking pagination in the future.

Version 2, edited 3 years ago by prosti (previous) (next) (diff)
Note: See TracTickets for help on using tickets.