Sunday, 2 February 2014

When to use WP_query(), query_posts() and pre_get_posts

If I'm making additional loops anywhere on a page—in the sidebar, in a footer, any kind of "related posts", etc—I want to be using WP_Query(). I can use that repeatedly on a single page without any harm. (right?).
What I don't know for sure
  1. When do I use @nacin's pre_get_posts vs. WP_Query? Should I use pre_get_posts for everything now?
  2. When I want to modify the loop in a template page — lets say I want to modify a taxonomy archive page — do I remove the if have_posts : while have_posts : the_post part and write my own WP_Query? Or do I modify the output using pre_get_posts in my functions.php file?
tl;dr
The tl;dr rules I'd like to draw from this are:
  1. Never use query_posts anymore
  2. When running multiple queries on a single page, use WP_Query
  3. When modifying a loop, do this ________.
Thanks for any wisdom
Terry
ps: I have seen and read: When should you use WP_Query vs query_posts() vs get_posts()? Which adds another dimension — get_posts. But doesn't deal with pre_get_posts at all.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More