Created
August 10, 2015 09:39
-
-
Save lmartins/226277a2fbaccf3c5de3 to your computer and use it in GitHub Desktop.
Query posts using the same taxonomy as the current post
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
/** | |
* 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