WordPress allows us to filter data by custom fields. The
WP_Meta_Query class is used to generate SQL
WHERE clauses to filter queries by meta fields. Now after we announced our WP_Date_Query Generator, the next natural move was to let you easily create advanced meta queries using our new WP_Meta_Query Generator.
Filtering by Custom Fields
WordPress allows you to filter posts based on post parameters like publish date, author, status, type, ect. Filter posts based on all kinds of taxonomies And filter posts based on other default values. But that is not always enough and you might need some extra data which is your own data – your own custom meta fields.
Custom fields are additional data fragments added to the main post, they are saved in a separate table –
wp_postmeta. This is also applies to comments, users and terms each of them with as a separate meta own table.
When retrieving posts / users / comments filtered by meta data it has a performance toll as you use SQL
JOIN. But in most cases the benefits outweigh the disadvantages. To reduce performance impact, you should not use the your own SQL statements but use
WP_Meta_Query class which implements internal cache.
Retrieving filtered data with
WP_Query and other supported query classes is easy squeezy. To filter with custom fields we will use the
meta_query argument. As a developer you all you have to do is pass a valid array. The rest will be handled by
WP_Meta_Query class which won’t return data, it will generate an SQL
WHERE clause to be used in the main query.
The Meta Query Generator
With the completion of our latest WP_Date_Query tool, we went straight away to build the Meta Query tool to help newbie developers and advanced coders to create custom meta queries, save them to your account and integrate to other queries.
You can use the Generator to learn the query internals or use it in your workflow to create simple queries or very complex queries with multiple rules.
The generator also has a builtin support for nested queries, allowing you to combine several meta queries inside each other to retrieve very specific result. WordPress even knows how to handle nested queries for you.
Imagine we have an e-commerce website with thousands of products. Lets say we need to filter only the blue items.
Now we are going to use multiple filters. Lets say we need to filter items by color – to retrieve only blue OR red items. We will need to use an “OR”
Lets take those blue/red items, and add a price parameter to show items under 500 USD (numeric field type). We will use nested snippet from the previous example.
Finally we are going to wrap it all up in to our
WP_Query query. We will use the meta query generated above in our general query.
The same principle can be applied on the
WP_Comment_Query. Lets see an example, a comment with a “review” type and a under 5 star rating.
WP_User_Query. Lets retrieve all the users from the city of Montreal in Canada.
This is a very simple-to-use class with many powerful capabilities that lets you create simple to very complex filters. All those capabilities are now integrated into our generators. Happy Filtering.