0
0
Wordpressframework~5 mins

Tax queries and meta queries in Wordpress

Choose your learning style9 modes available
Introduction

Tax queries and meta queries help you find posts based on categories, tags, or custom fields. They make your website show exactly what visitors want to see.

You want to show posts from a specific category or tag on a page.
You need to filter posts by custom fields like price or color.
You want to combine filters, like posts in a category AND with a certain custom field value.
You want to build a search that looks for posts with specific tags and meta data.
You want to create a custom list of posts based on user choices.
Syntax
Wordpress
$query = new WP_Query([
  'tax_query' => [
    [
      'taxonomy' => 'category',
      'field' => 'slug',
      'terms' => ['news', 'updates'],
      'operator' => 'IN'
    ]
  ],
  'meta_query' => [
    [
      'key' => 'price',
      'value' => 100,
      'compare' => '>=',
      'type' => 'NUMERIC'
    ]
  ]
]);

tax_query filters posts by taxonomy terms like categories or tags.

meta_query filters posts by custom fields (meta data) like price or color.

Examples
Find posts in the 'news' category.
Wordpress
$query = new WP_Query([
  'tax_query' => [
    [
      'taxonomy' => 'category',
      'field' => 'slug',
      'terms' => ['news']
    ]
  ]
]);
Find posts where the custom field 'color' equals 'blue'.
Wordpress
$query = new WP_Query([
  'meta_query' => [
    [
      'key' => 'color',
      'value' => 'blue',
      'compare' => '='
    ]
  ]
]);
Find posts tagged 'featured' with a rating of 4 or higher.
Wordpress
$query = new WP_Query([
  'tax_query' => [
    [
      'taxonomy' => 'post_tag',
      'field' => 'slug',
      'terms' => ['featured']
    ]
  ],
  'meta_query' => [
    [
      'key' => 'rating',
      'value' => 4,
      'compare' => '>='
    ]
  ]
]);
Sample Program

This code finds posts in the 'news' category that have a custom field 'featured' set to 'yes'. It prints the titles of these posts or says no posts found.

Wordpress
<?php
$args = [
  'post_type' => 'post',
  'tax_query' => [
    [
      'taxonomy' => 'category',
      'field' => 'slug',
      'terms' => ['news']
    ]
  ],
  'meta_query' => [
    [
      'key' => 'featured',
      'value' => 'yes',
      'compare' => '='
    ]
  ]
];
$query = new WP_Query($args);

if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    echo get_the_title() . "\n";
  }
} else {
  echo "No posts found.\n";
}
wp_reset_postdata();
?>
OutputSuccess
Important Notes

Always use wp_reset_postdata() after custom queries to avoid conflicts.

You can combine multiple taxonomies and meta queries using arrays for complex filters.

Use operator in tax queries to control how terms are matched (e.g., IN, NOT IN).

Summary

Tax queries filter posts by categories, tags, or custom taxonomies.

Meta queries filter posts by custom fields (meta data).

Combining tax and meta queries helps create precise post filters.