Skip to content

Instantly share code, notes, and snippets.

@lmartins
Created August 10, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmartins/226277a2fbaccf3c5de3 to your computer and use it in GitHub Desktop.
Save lmartins/226277a2fbaccf3c5de3 to your computer and use it in GitHub Desktop.
Query posts using the same taxonomy as the current post
/**
* QUERY DESIGNS DO VERTICAL
* http://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term
*/
$vertical_terms = wp_get_object_terms( $post->ID, 'vertical_cat' );
if( $vertical_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $vertical_terms > 1 ) )
$tax_query['relation'] = 'OR';
// loop through verticals and build a tax query
foreach( $vertical_terms as $term ) {
$tax_query[] = array(
'taxonomy' => 'vertical_cat',
'field' => 'slug',
'terms' => $term->slug,
);
}
$args = array(
'post_type' => 'vertical',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__not_in' => array( $post->ID ), // exclui o post que já está destacado
'tax_query' => $tax_query
);
$context['designs'] = Timber::get_posts( $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment