Created
November 12, 2012 18:40
-
-
Save rachelbaker/4061088 to your computer and use it in GitHub Desktop.
WordPress list posts that are in two different taxonomies
This file contains hidden or 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 | |
| // NOTE: replace the values of these variables with custom taxonomies | |
| $multi_term1 = 'taxonomy_term1'; | |
| $multi_term2 = 'taxonomy_term2'; | |
| // get all terms for taxonomy1 | |
| $multi1_terms = get_terms($multi_term1); | |
| foreach ( $multi1_terms as $multi1_term ) { | |
| $multi1_term1slugs[]= $multi1_term->slug; | |
| } | |
| // get all terms for taxonomy2 | |
| $multi2_terms = get_terms($multi_term2); | |
| foreach ( $multi2_terms as $multi2_term ) { | |
| $multi2_term2slugs[]= $multi2_term->slug; | |
| } | |
| // $multi_tax_query args | |
| $query_multi['posts_per_page'] = 10; | |
| $query_multi['tax_query'] = array( | |
| 'relation' => 'AND', | |
| array( | |
| 'taxonomy' => $multi_term1, | |
| 'terms' => $multi2_term1slugs, | |
| 'field' => 'slug' | |
| ), | |
| array( | |
| 'taxonomy' => $multi_term2, | |
| 'terms' => $multi2_term2slugs, | |
| 'field' => 'slug' | |
| ), | |
| ); | |
| // find posts that contain both of the two taxonomies | |
| $multi_tax_query = new WP_Query( $query_multi ); | |
| // output in list | |
| echo '<ul id="tax2-list">'; | |
| while ( $multi_tax_query->have_posts() ) : $multi_tax_query->the_post(); | |
| echo '<li><a href="'; | |
| the_permalink(); | |
| echo '">'; | |
| the_title(); | |
| echo '</a></li>'; | |
| continue; | |
| endwhile; | |
| echo '</ul>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment