Query Post By Custom Fields WordPress Advanced Custom Post
Query By a Custom Field Advanced Custom Fields Plugin
I was looking into searching custom fields on WordPress and I found a simple method for this.
$posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'post', 'meta_key' => 'color', 'meta_value' => 'red' ));
Here is also an advanced example on how to do it.
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'orange'),
'compare' => 'IN',
),
array(
'key' => 'featured',
'value' => '1',
'compare' => '=',
),
),
));