List Posts By Terms


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

To display events from The Events Calendar Pro organized by taxonomy


Copy this code and paste it in your HTML
  1. /**
  2. * Get posts and group by taxonomy terms.
  3. * @param string $posts Post type to get.
  4. * @param string $terms Taxonomy to group by.
  5. * @param integer $count How many post to show per taxonomy term.
  6. * based on https://gist.github.com/jaredkc/6191133
  7. */
  8.  
  9.  
  10. function list_posts_by_term( $atts ) {
  11.  
  12. $semester = get_option('the_muse_site_settings_current_semester');
  13. $current_semester = get_term( $semester[0], "semester" ); //current semester as set in options
  14.  
  15. extract(shortcode_atts(array(
  16. "posts" => "tribe_events",
  17. "terms" => "genre",
  18. "count" => 25
  19. ), $atts));
  20.  
  21.  
  22. $tax_terms = get_terms( array(
  23. 'taxonomy' => $terms,
  24. 'hide_empty' => true,
  25. ) );
  26.  
  27. $output = "<div class='auto-col-3'>";
  28.  
  29.  
  30. foreach ( $tax_terms as $theterm ) {
  31. $output .= '<h3><a name="'.$theterm->slug.'"></a>' . $theterm->name . '</h3> <ul>';
  32.  
  33. $args = array(
  34. 'posts_per_page' => $count,
  35. 'post_type' => $posts,
  36. 'tribeHideRecurrence' => true,
  37. 'start_date' => 'today',
  38. 'tax_query' => array(
  39. 'taxonomy' => $terms,
  40. 'field' => 'slug',
  41. 'terms' => $theterm->slug,
  42. ),
  43. 'taxonomy' => "semester",
  44. 'field' => 'slug',
  45. 'terms' => $current_semester->slug,
  46. 'operator' => "IN",
  47. ),
  48.  
  49. )
  50.  
  51. );
  52.  
  53. $tax_terms_posts = tribe_get_events($args);
  54.  
  55.  
  56. foreach ( $tax_terms_posts as $post ) {
  57. $output .= '<li><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
  58. }
  59. $output .= '</ul>';
  60. }
  61.  
  62. wp_reset_postdata();
  63. $output .= '</div>';
  64. return $output;
  65. }
  66.  
  67.  
  68. add_shortcode( 'list_grouped_posts', 'list_posts_by_term' );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.