I read @nacin's You don't know Query yesterday and was sent down a bit of a querying rabbit hole. Before yesterday, I was (wrongly) using query_posts()
for all my querying needs. Now I'm a little bit wiser about using WP_Query()
, but still have some gray areas.
What I think I know for sure:
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
- When do I use @nacin's
pre_get_posts
vs.WP_Query()
? Should I usepre_get_posts
for everything now? - 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 ownWP_Query()
? Or do I modify the output usingpre_get_posts
in my functions.php file?
tl;dr
The tl;dr rules I'd like to draw from this are:
- Never use
query_posts
anymore - When running multiple queries on a single page, use
WP_Query()
- 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.