We’re rewarding the question askers & reputations are being recalculated! Read more.
2 Typo
source | link

There are two different contexts for loops:

  • main loop that happens based on URL request and is processed before templates are loaded
  • secondary loops that happen in any other way, called from template files or otherwise

Problem with query_posts() is that it is secondary loop that tries to be main one and fails miserably. Thus forget it exists.

To modify main loop

  • don't use query_posts()
  • use pre_get_posts filter with $query->is_main_query() check
  • alternately use request filter (a little too rough so above is better)

To run secondary loop

Use new WP_Query or get_posts() which are pretty much interchangeable (latter is thin wrapper for former).

To cleanup

Use wp_reset_query() if you used query_posts() or messed with global $wp_query directly - so you will almost never need to.

Use wp_rest_postdatawp_reset_postdata() if you used the_post() or setup_postdata() or messed with global $post and need to restore initial state of post-related things.

There are two different contexts for loops:

  • main loop that happens based on URL request and is processed before templates are loaded
  • secondary loops that happen in any other way, called from template files or otherwise

Problem with query_posts() is that it is secondary loop that tries to be main one and fails miserably. Thus forget it exists.

To modify main loop

  • don't use query_posts()
  • use pre_get_posts filter with $query->is_main_query() check
  • alternately use request filter (a little too rough so above is better)

To run secondary loop

Use new WP_Query or get_posts() which are pretty much interchangeable (latter is thin wrapper for former).

To cleanup

Use wp_reset_query() if you used query_posts() or messed with global $wp_query directly - so you will almost never need to.

Use wp_rest_postdata() if you used the_post() or setup_postdata() or messed with global $post and need to restore initial state of post-related things.

There are two different contexts for loops:

  • main loop that happens based on URL request and is processed before templates are loaded
  • secondary loops that happen in any other way, called from template files or otherwise

Problem with query_posts() is that it is secondary loop that tries to be main one and fails miserably. Thus forget it exists.

To modify main loop

  • don't use query_posts()
  • use pre_get_posts filter with $query->is_main_query() check
  • alternately use request filter (a little too rough so above is better)

To run secondary loop

Use new WP_Query or get_posts() which are pretty much interchangeable (latter is thin wrapper for former).

To cleanup

Use wp_reset_query() if you used query_posts() or messed with global $wp_query directly - so you will almost never need to.

Use wp_reset_postdata() if you used the_post() or setup_postdata() or messed with global $post and need to restore initial state of post-related things.

1
source | link

There are two different contexts for loops:

  • main loop that happens based on URL request and is processed before templates are loaded
  • secondary loops that happen in any other way, called from template files or otherwise

Problem with query_posts() is that it is secondary loop that tries to be main one and fails miserably. Thus forget it exists.

To modify main loop

  • don't use query_posts()
  • use pre_get_posts filter with $query->is_main_query() check
  • alternately use request filter (a little too rough so above is better)

To run secondary loop

Use new WP_Query or get_posts() which are pretty much interchangeable (latter is thin wrapper for former).

To cleanup

Use wp_reset_query() if you used query_posts() or messed with global $wp_query directly - so you will almost never need to.

Use wp_rest_postdata() if you used the_post() or setup_postdata() or messed with global $post and need to restore initial state of post-related things.