Skip to content
wp-skills
Register
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.

Comma separated. One value emits a string, several emit an array.

Only emitted when there is more than one clause — with a single clause it has nothing to relate.

Taxonomy clauses1
  • #1

    The ID fields are compared as numbers; a quoted slug against 'Term ID' matches nothing.

    Comma separated. Not used by the EXISTS operators.

    On by default in WordPress; only emitted when you turn it off.

Reads the page number the main query does not set for a secondary query.

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' ) );
}