Opened 8 years ago
Closed 5 years ago
#39483 closed enhancement (wontfix)
Use $wp_the_query for the main loop and pagination
Reported by: |
|
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
.
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.
Change History (6)
#2
follow-up:
↓ 3
@
8 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.
#3
in reply to:
↑ 2
@
8 years ago
Replying to prosti:
query_posts()
and altering the main loop? Some believe that this is not so good idea.
Instead they advicepre_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
@
8 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.
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?