Skip Navigation


Searching custom fields in WordPress with meta_query

Author: JLB

Published On: February 22, 2012

There have been quite a few incredibly useful features that were introduced in WordPress version 3.0, which came out over a year ago. However, perhaps my favorite didn’t come till half a year later when version 3.1 was released.

Previously, searching posts, pages or custom post types using custom field required a more-than-reasonable amount of work by directly querying the database with lots of nested SQL, which was fairly cumbersome. Fortunately, a new parameter was introduced into the query string arsenal streamlining this process.

If you look in the WordPress Codex for WP_Query, you’ll see a new(ish) parameter called ‘meta_query’, which does exactly that–queries meta data for a post. It can search singular fields, as well as multiple fields, and handles all your typical SQL operators, e.g., =, !=, LIKE, BETWEEN, and IN. As a bonus, meta_query also plays nicely with the search parameter, ‘s’, and works with custom post types and custom taxonomies.

There are two ways it can be used.

For singular queries:

$query = new WP_Query( array ( 'meta_key' => 'meta_value' ) );

For multiple queries:

$query = new WP_Query( array(
	'post_type' =>; 'food',
	'meta_query' => array(
			'key' => 'color',
			'value' => 'brown',
			'compare' => 'NOT LIKE'
			'key' => 'taste',
			'value' => 'sweet'
			'compare' => '='

Note that in that first example, the ‘compare’ key is left out in the second array. If nothing is set for ‘compare’, the default value of ‘=’ will be used.

It’s remarkable working with the team that treats your business they would their own with drive, innovation, humility and a shared love of bringing great things to the wonderful Franklin & Nashville community.


RED DOG WINE & SPIRITS – Nashville Web Design


“In the search of looking high and low for a Ecommerce website design firm, we came across JLB. They are now like a part of our family and strive to do all they can to take our sales and growth to a whole new level. We recommend JLB to everyone we come across looking for a solution to their website needs.”


Chad, BOWADX – Franklin TN Web Design

“JLB has been the driving force  that has launched our company into the media spotlight, gaining us publicity through a broad cross-section of business related media including new apps and interactive activity for our clients. We are moving forward with our second web design with JLB and will continue to make them part of our team.”


John, The Perch – Brentwood TN Web Design

“I started with JLB when they first opened fifteen years ago, when they redesigned a website for me.  They are my go-to company for anything to do with web sites. These guys are professional, talented, creative and an energetic team. I will always go to JLB first for my web design work. Thanks JLB.”


Phyllis, Carneros Winery – Nashville TN Web Design

We were having problems with our online brand and digital marketing. JLB was great. Worked through it with us, created an amazing presence and business is great. The staff at JLB is fantastic too. Friendly and always there for you.


Life Line Repairs – Nashville TN Web Design

I and all of us at Sports & Entertainment Nashville TN have been thrilled with the website you guys did for us, and especially the professional service and communication you guys have shown.


Sports and Entertainment – Nashville Web Design

We interviewed several web design firms, but found that JLB’s talented team was the most creative, innovative, and professional. Choosing JLB has been a great decision! We are fortunate to have JLB be a part of the BMC Metalwork’s future as we continue to enhance our business.


Jimmy, BMC – Franklin TN Web Design

“There were a number of website design companies in Nashville that would have done an adequate job for our company. The difference was that JLB wanted to be our partner and has been for 11 years. STAR Physical Therapy has been blessed to have them as a teammate.”


Regg Swanson, STAR – Franklin TN Web Design