bloqhead
10/1/2013 - 2:44 PM

Example WP query that excludes all terms from a custom taxonomy.

Example WP query that excludes all terms from a custom taxonomy.

<?php
/**
* Taxonomy Exclusion
**/
$tax = 'artists';
$terms = array();
$custom_terms = get_terms( $tax, array( 'fields' => 'names' ) );
if( is_array( $custom_terms ) ) {
	$terms = $custom_terms;
}
/**
* WP Query
**/
$custom_query = new WP_Query(
	array(
		'post_type' => 'custom_post_type',
		'posts_per_page' => -1, // display all posts within the CPT
		'orderby' => 'menu_order',
		'order' => 'ASC',
		'force_no_custom_order' => TRUE,
		'post_status' => 'publish',
		'tax_query' => array( array(
				'taxonomy' => $tax,
				'terms' => $terms,
				'field' => 'slug',
				'operator' => 'NOT IN'
			)
		)
	)
);
while ( $custom_query->have_posts() ) : $custom_query->the_post(); $slug = $post->post_name;
?>