Skip to content

Instantly share code, notes, and snippets.

@colegeissinger
Last active August 29, 2015 13:56
Show Gist options
  • Save colegeissinger/9061223 to your computer and use it in GitHub Desktop.
Save colegeissinger/9061223 to your computer and use it in GitHub Desktop.
Query post types based on taxonomy
<?php
// The query variable
$queried_term = get_query_var( 'Clients' );
// Return the terms for current post based off the query variable. Make you sanitize your queries!!
$terms = wp_get_post_terms( absint( get_the_ID() ), 'Clients', array( 'fields' => 'all' ) );
// Start the arguments for WP_Query(). We'll define an empty tax_query
// so we hav something to dump our terms into programatically.
$args = array(
'post_type' => 'post',
'tax_query' => array()
);
// Loop through all the terms returned for the post and add them to
// the tax_query in the proper format
foreach ( $terms as $term ) {
$args['tax_query'][] = array(
'taxonomy' => 'Clients',
'field' => 'slug',
'terms' => sanitize_title( $term->slug )
);
}
// Fetch the posts we need
$client_posts = new WP_Query( $args );
// Start the loop
if ( $client_posts->have_posts() ) : while ( $client_posts->have_posts() ) : $client_posts->the_post();
// Your Loop Content Here
endwhile;
else :
// Handle the loop when no posts exist
endif;
// FIN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment