Filtering data with WP_Meta_Query

Filtering data with WP_Meta_Query

Online by J&S Code
13 July 2017 | PHP WP snippets, code & hacks | 323 recognitions

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 new WP_Meta_Query Generator.

Filtering data with WP_Meta_Query

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.


Meta Query Parameters
Meta Query Parameters

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.


Nested WP_Meta_Query
Nested WP_Meta_Query


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” compare operator.

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.

Lastly the WP_User_Query. Lets retrieve all the users from the city of Montreal in Canada.

Wrap up

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.

About The Author

J&S Code

Sorin C is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

Put here your thoughts

Your email address will not be published. Please complete all fields * correctly You may use these HTML tags: bold text, italicised text, and paragraphs.

On the same idea

Securing WordPress AJAX Forms Using Nonce
Online by stevenmedia | 7 August 2018

When creating a WordPress theme or plugin, AJAX is often used in order to enhance the user experience. In order to ensure security and protect your site against several types...

AJAX for WordPress
Online by stevenmedia | 7 August 2018

This tutorial is just a simple guide to understand the basic about using AJAX in WordPress. (more…)

Customizing the WordPress Query
Online by stevenmedia | 6 August 2018

One of the most powerful features of WordPress is the WP Query. It is what determines what content is displayed on what page. And often you’ll want to modify this...