tools/wordpress-tax-query-generator
WordPress Tax Query Generator
Build a WP_Query filtered by taxonomy terms — multiple clauses with AND or OR, term IDs typed as numbers, and the EXISTS operators handled the way WordPress actually reads them.
snippet.php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'news' ),
'operator' => 'IN',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
printf(
'<li><a href="%s">%s</a></li>',
esc_url( get_permalink() ),
esc_html( get_the_title() )
);
}
echo '</ul>';
// Puts the global $post back to the main query's post, which the_post()
// overwrote. Without this every template tag after the loop — the page
// title, the comment template, anything calling get_the_ID() — reads the
// last post looped over here instead.
wp_reset_postdata();
} else {
printf( '<p>%s</p>', esc_html__( 'Nothing found.', 'text-domain' ) );
}