How to Sort Posts by their Taxonomy Terms
Don’t Cache WP_Query Objects (and Why Not)
Recently, I was working on site speed for a client with one page that makes a large number of custom database queries through WordPress’s WP_Query API.
How to Paginate a Custom WP_Query: Stack Overflow to the Rescue
Something I can’t remember needing to do before is to paginate a custom WP_Query. (Maybe my memory’s failing me, since this is what you’d need to know how to do to put post content at the top of an archive page—one of the weirdest and most common “Why can’t WordPress do that?” things in my experience.)
How to Write a WP_Query for Multiple Specific Post IDs
Recently, a client project I’m working on required something I don’t think I’ve done before: write a WP_Query to fetch a dynamically generated list of post IDs.
Creating a WordPress Widget to Show a Random Post
Making a widget to show a random post uses two underlying concepts you need to get familiar with to be good at WordPress development: making use of WP_Query objects, and making widgets. If you’ve not checked them before, check out our Quick Guides which more thoroughly introduce both:
Making your First WordPress Plugin (From Scratch): Live Training Tomorrow
We’re doing a WordPress development training session tomorrow!
Using WP_Query Your First Time: A Shortcode to Show Post Titles
Using WP_Query for the first time can be scary. If you’re an established programmer who is well acquainted with object-oriented programming, it’s pretty easy. But if you’re not you need to use WordPress’s looping concept, and an object, for the first time, and it’s a lot to take in. Don’t get overwhelmed. Copying and pasting the first few (dozen) times is how everyone learns. My hope is that the video gives you some understanding though:
WP_Query vs query_posts() vs get_posts()
This is not a new resource, but I just rediscovered and found it still incredibly relevant. Back in 2011, Andrey “Rarst” Savchenko put together a little diagram that shows the difference between three of the most common ways of dealing with getting posts in WordPress, a new WP_Query object, or the query_posts() or get_posts() functions.
Using WP_Query Objects Without the Loop
I really enjoyed David’s article from a few weeks ago on the basics of functional programming as it applies to WordPress’s nested data structures. One of the nice things about working with him is that I learn a lot, too. Today, I want to continue exploring the somewhat-functional world David introduced, and suggest some freer ways of interacting with a very important entity in WordPress: the WP_Query object.
Getting to Know WP_Query
WP_Query is an exceptionally powerful tool in WordPress. Every WordPress site contains a database that stores many posts (of many post types) that make up the bulk of that site’s content, and WP_Query is the best way to fetch or retrieve a given selection of those posts for processing.