Loop through Wordpress 3.0+ custom post type taxonomies


/ Published in: PHP
Save to your folder(s)

Wordpress 3.3 custom post type query that will get all of the taxonomies, loop through them, and output the posts from each.


Copy this code and paste it in your HTML
  1. //for a given post type, return all
  2. $post_type = 'energy-link';
  3. $tax = 'category';
  4. $tax_terms = get_terms($tax);
  5. if ($tax_terms) {
  6. foreach ($tax_terms as $tax_term) {
  7. echo '
  8. <div class="wrap_the_tax">
  9. ';
  10.  
  11. $args=array(
  12. 'post_type' => $post_type,
  13. 'tax_query' => array(
  14. 'taxonomy' => $tax,
  15. 'field' => 'slug',
  16. 'terms' => $tax_term->slug
  17. )
  18. ),
  19. 'post_status' => 'publish',
  20. 'posts_per_page' => -1,
  21. 'orderby' => 'title',
  22. 'order' => 'ASC',
  23. 'operator' => 'IN'
  24. ); // END $args
  25.  
  26. $my_query = null;
  27. $my_query = new WP_Query($args);
  28.  
  29. //debug($my_query);
  30.  
  31. if( $my_query->have_posts() ) {
  32. while ($my_query->have_posts()) : $my_query->the_post();
  33. echo sces_smallblock( $post->post_title, $post->post_excerpt);
  34. endwhile;
  35. } // END if have_posts loop
  36. wp_reset_query();
  37. echo '
  38. </div>
  39. ';
  40. } // END foreach $tax_terms
  41. } // END if $tax_terms

Report this snippet


Comments


You need to login to view comments.