Skip to content

Instantly share code, notes, and snippets.

@bloqhead
Created October 1, 2013 14:44
Show Gist options
  • Save bloqhead/6779606 to your computer and use it in GitHub Desktop.
Save bloqhead/6779606 to your computer and use it in GitHub Desktop.
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;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment