preloader

Using WP_Query with Custom Fields: Meta Query Examples

Querying posts by custom field values is one of the most common WordPress development tasks. Custom Field Finder helps you confirm exactly what meta keys are stored so you can write accurate queries. Here are the essential meta_query patterns.

Basic Meta Query: Posts Where a Field Equals a Value

$args = array(
    'post_type'  => 'post',
    'meta_query' => array(
        array(
            'key'     => 'featured',
            'value'   => '1',
            'compare' => '=',
        ),
    ),
);
$query = new WP_Query( $args );

This retrieves posts where the meta key “featured” has the value “1”. Note: meta values are stored as strings even if you saved an integer or boolean. Use string comparisons unless you specify NUMERIC type.

Meta Query: Posts Where Field Exists

$args = array(
    'post_type'  => 'property',
    'meta_query' => array(
        array(
            'key'     => 'sale_price',
            'compare' => 'EXISTS',
        ),
    ),
);

Returns posts where the meta key exists regardless of its value. Use NOT EXISTS to find posts where the key has not been set.

Numeric Comparison: Greater Than, Less Than

$args = array(
    'post_type'  => 'property',
    'meta_query' => array(
        array(
            'key'     => 'price',
            'value'   => '500000',
            'compare' => '<=',
            'type'    => 'NUMERIC',
        ),
    ),
);

Without ‘type’ => ‘NUMERIC’, the comparison is alphabetical (string comparison) rather than numeric. Always specify NUMERIC for integer or decimal field values. Other type options: DATE, DATETIME, TIME, CHAR, BINARY.

Multiple Conditions: AND Logic

$args = array(
    'post_type'  => 'property',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'price',
            'value'   => array( 200000, 500000 ),
            'compare' => 'BETWEEN',
            'type'    => 'NUMERIC',
        ),
        array(
            'key'     => 'bedrooms',
            'value'   => '3',
            'compare' => '>=',
            'type'    => 'NUMERIC',
        ),
    ),
);

Use BETWEEN to query within a range. The value must be an array with exactly two elements.

Multiple Conditions: OR Logic

$args = array(
    'post_type'  => 'event',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'   => 'city',
            'value' => 'London',
        ),
        array(
            'key'   => 'city',
            'value' => 'Manchester',
        ),
    ),
);

Ordering by Meta Value

$args = array(
    'post_type'  => 'product',
    'meta_key'   => 'price',
    'orderby'    => 'meta_value_num',
    'order'      => 'ASC',
    'meta_query' => array(
        array(
            'key'     => 'price',
            'compare' => 'EXISTS',
        ),
    ),
);

Use meta_value_num for numeric ordering, meta_value for alphabetical ordering. The meta_key parameter specifies which key to order by.

Performance Note

meta_query joins the wp_postmeta table, which is not indexed for arbitrary key queries on large sites. For queries run frequently on large datasets (thousands of posts), consider adding a custom database index on the meta_key column, or storing frequently queried values in a dedicated taxonomy which has better query performance than postmeta.

For complex custom query implementations, custom database indexes, and high-performance WordPress data retrieval, a WordPress developer can design and implement the right data architecture for your project.

Keep Reading

Previous Post How to Debug ACF Fields Not Saving or Displaying in WordPress Next Post How to Export WordPress Users to CSV and Import Into Mailchimp or Klaviyo

Need Help With Your WordPress Site?

If you need help with WordPress fixes, plugin issues, theme customization, or development work, feel free to get in touch.

Get a Free Estimate