Skip to content

Instantly share code, notes, and snippets.

@emaildano
Created March 9, 2014 15:48
Show Gist options
  • Save emaildano/9449764 to your computer and use it in GitHub Desktop.
Save emaildano/9449764 to your computer and use it in GitHub Desktop.
Group posts by taxonomy.
<?php
$get_current_cat = get_term_by('name', single_cat_title('',false), 'category');
$current_cat = $get_current_cat->term_id;
// List posts by the terms for a custom taxonomy of any post type
$post_type = 'special_events';
$tax = 'locations';
$tax_terms = get_terms( $tax, 'orderby=name&order=ASC');
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<div class="clearfix">
<div class="btn-group bootstrap-select shortcode-dropdown-lists">
<button type="button" class="btn dropdown-toggle" data-toggle="dropdown">
<span class="filter-option pull-left"><?php echo $tax_term->name; ?></span> <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<?php $term_list = wp_get_post_terms($post->ID, 'category', array("fields" => "ids")); ?>
<?php if (in_array($current_cat, $term_list) ): ?>
<li><?php the_title(); ?></li>
<?php endif; // if in_array ?>
<?php endwhile; // end of loop ?>
</ul>
</div>
</div>
<?php endif; // if have_posts()
wp_reset_query();
} // end foreach #tax_terms
} // end if tax_terms
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment