RS Bloggers

What is Meta Query in WordPress?

Introduction to Meta Query in WordPress

If you’ve ever built a WordPress site with Custom Post Types or Advanced Custom Fields, you’ve probably faced a situation where you need to filter posts based on certain custom field values.

That’s where meta_query comes in.

In simple terms, meta_query lets you query posts by custom field values stored in the WordPress postmeta table. Whether you’re building a real estate listing, product catalog, or filtering events by date — meta_query gives you powerful control.

When Should You Use Meta Query?

Imagine you’re building a real estate website. You have a Custom Post Type called property with custom fields like:

  • price

  • location

  • bedrooms

You want to show only properties with at least 3 bedrooms. You can’t achieve this with the default WordPress queries — but meta_query makes it easy.

Basic Example of Meta Query

Here’s a practical example of how to use meta_query:

<?php
  $args = array(
    'post_type' => 'property',
    'meta_query' => array(
        array(
            'key'     => 'bedrooms',
            'value'   => 3,
            'compare' => '>='
        )
    )
);

$query = new WP_Query( $args );

if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        the_title();
    }
    wp_reset_postdata();
}

In this example:
 We’re querying the property post type
 We only fetch properties where the bedrooms custom field is greater than or equal to 3

Multiple Conditions Using Meta Query

You can also combine multiple conditions, like filtering properties with at least 3 bedrooms AND located in “New York”:

  $args$args = array(
    'post_type' => 'property',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'bedrooms',
            'value'   => 3,
            'compare' => '>='
        ),
        array(
            'key'     => 'location',
            'value'   => 'New York',
            'compare' => '='
        )
    )
);

Where Are Meta Fields Coming From?

Custom fields (meta fields) are additional data stored for each post. You can create them:

  •  Manually via WordPress’ Custom Fields UI
  •  Using plugins like Advanced Custom Fields (ACF)
  •  When developing Custom Post Types

Want to Learn More?

If you’re new to Custom Post Types or Custom Taxonomies, start here:

👉 How to Create Custom Post Types in WordPress

👉 How to Create Custom Taxonomies in WordPress

Once you master CPTs and taxonomies, combining them with meta_query unlocks endless possibilities for building dynamic, filterable content.