Last active
August 29, 2015 13:56
-
-
Save colegeissinger/9061223 to your computer and use it in GitHub Desktop.
Query post types based on taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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