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( array( 'key' => 'color', 'value' => 'brown', 'compare' => 'NOT LIKE' ), array( '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.
Recent News
Benefits of an Intuitive Website Design
Did you know that 75% of people base the credibility…
Understanding how to Target Your Audience
Did you know that it takes about 50 milliseconds for…
How Will Graphic Design Boost Your…
As 73% of businesses are now investing in graphic design…
Why Blogging Should Still be a…
There are 89,409 Google searches per second, which means your…
Post Categories
- Company News (16)
- Content Marketing (14)
- Copywriting (4)
- Digital Marketing (38)
- E-Commerce (10)
- Email Marketing (7)
- Featured (4)
- Graphic Design (15)
- Local SEO (6)
- Logo Design (2)
- Marketing (10)
- Mobile Optimization (2)
- Pay Per Click (8)
- Search Engine Marketing (2)
- Search Engine Optimization (48)
- Social Media (19)
- Uncategorized (3)
- Video (2)
- Web Design (50)
- Web Development (29)
- Website Support (6)