Working with WP_Query, especially when you’re doing some custom work outside of the usual “get some posts and display them on a template” can be powerful. This is especially true of some of the advanced arguments (like using WP_Meta_Query, for example).
It’s also kind of nice that setting up the process has a standard way of doing things. Namely:
- Define the arguments,
- Instantiate WP_Query,
- Check if there are posts,
- Loop through them,
- Finish Them.
But if you get to where you’re doing any advanced work such as working with a custom post type from a third-party solution, having to sideload media, determine if something exists before actually doing any work with it, then it can be a little more complicated to work with, can’t it?
I’ve found that, like with anything in programming, breaking it down into much more readable modules (or functions or pieces or whatever you’d like to call them) can make it much easier to work with.
So here’s one way that I go about working to make WP_Query readability improvements in a variety of the stuff I’ve done lately.