Make WordPress Core

Opened 7 years ago

Closed 4 years ago

#39483 closed enhancement (wontfix)

Use $wp_the_query for the main loop and pagination

Reported by: prosti's profile prosti Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords: 2nd-opinion
Focuses: Cc:

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 (6)

#1 @SergeyBiryukov
7 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
7 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 7 years ago by prosti (previous) (diff)

#3 in reply to: ↑ 2 @SergeyBiryukov
7 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
7 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.

Custom loops are already possible using new WP_Query()

And it will be in the future.

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

#5 @Howdy_McGee
4 years ago

  • Keywords 2nd-opinion added

The paginate_links() appears to only rely on $wp_query to manage max_num_pages which can be altered by passing total => $custom_query->max_num_pages. I don't think this improvement is necessary and can probably be closed.

#6 @SergeyBiryukov
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.