Query Multiple Taxonomies

Working with large sites that have cross-over content, sometimes it’s necessary to know how to bring multiple taxonomies into a single query.

$myquery['tax_query'] = array(
		    
		    'relation' => 'OR',
		    array(
		        'taxonomy' => 'category',
		        'terms' => array('winnipeg'),
		        'field' => 'slug',
		    ),
		    array(
		        'taxonomy' => 'event',
		        'terms' => array('manitoba'),
		        'field' => 'slug',
		    )
		);
		
		query_posts($myquery);

Sourced here.

Or, if you need to target specific Custom Post Types (or exclude others):

$myquery = array(
	'post_type' => array( 'post', 'events' ),
	'tax_query' => array(
	'relation' => 'OR',
	array(
		'taxonomy' => 'category',
		'terms' => 'calgary-3',
		'field' => 'slug'
	),				
	array(
		'taxonomy' => 'event-region',
		'terms' => 'national',
		'field' => 'slug'
	),
	array(
		'taxonomy' => 'event-region',
		'terms' => 'calgary',
		'field' => 'slug'
	)
	)
);

 

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.