Alright, it’s time to continue our journey building our awesome
WP_Query_Builder class! So far, we’ve only designed it to build simple WordPress queries. But not all WordPress queries are simple or easy to model using just a cascading method.
It’s even possible that you’ve run into the limits of the
WP_Query_Builder already. The class that we created then wasn’t a complete solution. It had some serious limitations if you were a
WP_Query expert. You couldn’t use it to perform complex WordPress queries.
What do we mean by complex WordPress queries? We mean queries that use a complex
WP_Query query parameters. At the moment, there are three of them:
So, for this article, we’re going to back to our
WP_Query_Builder class. We’ll add support for one of these complex
WP_Query query parameters. We won’t go over all three due to how complex they are. But you can apply what you’ve seen for this one query parameter to design a fluent interface for the other two.
Continue reading Designing a class to create complex WordPress queries
In a previous article, we looked at how you could create a class to manage WordPress posts. We ended creating a class who’s job it was to interact with the WordPress database. And it did that quite well!
As part of its job, our class also had to be able to query the WordPress database. To do that, we made it easier to reuse code around the
WP_Query class. We achieved that by creating methods that used predefined query arguments.
But have you ever looked at the codex page for
WP_Query? Holy Moley, there are a lot of query parameters in there! It’s pretty intimidating and not always easy to use in practice.
That’s the problem that we’re going to look at in this article. We’ll design a class to simplify how we build
WP_Query objects. It’ll handle all the complexity around
WP_Query query parameters for you. The result should be an easier way for you for you to create WordPress queries.
Continue reading Designing a class to build simple WordPress queries
It’s not too far-fetched to see WordPress as a library. You write a post and publish it. Meanwhile, WordPress classifies it and puts it away on its shelves.
But how do you find your post again after WordPress shelved it away? You need someone to help you navigate this huge library and find what you’re looking for. You need a librarian!
That’s the job of the
WP_Query class in a nutshell. It’s the librarian of the WordPress database. You talk to it when you want to search through the WordPress database. It’ll help you find the information that you need!
It does this without requiring that you know how WordPress stores that information. That means that you don’t have to know how to write MySQL queries.
WP_Query takes care of all that for you. (Yay!) It’ll transform your search request into a safe MySQL query and process it for you.
But how does the
WP_Query class do this? That’s what we’re going to look at today. We’re going to explore how it works and what it can do for you. This will help you leverage it to the maximum in the future.
Continue reading WordPress for the adventurous: WP_Query class
When building a plugin, it’s not uncommon to need to fetch posts from the database. After all, you might be using your own custom post type. Or maybe, you built a special functionality on top of posts and you want to find them that way. Those are just a few reasons why your plugin might need to query WordPress for posts.
Regardless of the reason, the standard way of doing that is to use the
WP_Query class. This lets us create WordPress queries in a safe way outside the loop. The problem is that they’re not easy to reuse.
For example, let’s say that you want a query to fetch only one result. You’re always going to need to add the
'posts_per_page' => 1 and
'no_found_rows' => true query arguments. That last one removes the
SQL_CALC_FOUND_ROWS query used by WordPress pagination. (You don’t need to paginate one result!) This lets us improve performance a bit by removing the unnecessary query.
Now, you’d need to copy these two query arguments whenever you create a query to fetch a single result. This is far from ideal. So let’s look at designing a solution to this problem. Using object-oriented programming, of course!
Continue reading Designing a class to manage WordPress posts