Skip to content

Instantly share code, notes, and snippets.

@xtrasmal
Created May 21, 2013 20:28
Show Gist options
  • Select an option

  • Save xtrasmal/5622962 to your computer and use it in GitHub Desktop.

Select an option

Save xtrasmal/5622962 to your computer and use it in GitHub Desktop.
WordPress Loop posts per post tag
<?php
$terms = get_terms("post_tag");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo '<h2>' . $term->name . '</h2>';
echo '<ul>';
$loop = new WP_Query( array(
'post_type' => 'product',
'post_per_page' => 100,
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $term->term_id
)
)
));
// the loop
while ($loop->have_posts()) : $loop->the_post();
// do loop content
echo '<li>' . get_the_title() . '</li>';
endwhile;
// reset $post so that the rest of the template is in the original context
wp_reset_postdata();
echo '</ul>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment