Return to Snippet

Revision: 80123
at January 29, 2020 20:11 by reflectiondigital


Initial Title
List Posts By Terms

Initial Description
To display events from The Events Calendar Pro organized by taxonomy

Initial Code
/**
* Get posts and group by taxonomy terms.
* @param string $posts Post type to get.
* @param string $terms Taxonomy to group by.
* @param integer $count How many post to show per taxonomy term.
* based on https://gist.github.com/jaredkc/6191133
*/


function list_posts_by_term( $atts ) {
	
	$semester = get_option('the_muse_site_settings_current_semester');
	$current_semester = get_term( $semester[0], "semester" ); //current semester as set in options
		
	extract(shortcode_atts(array(
	    "posts" => "tribe_events",
	    "terms"	=> "genre",
	    "count"	=> 25
	  ), $atts));
	
		
	$tax_terms = get_terms( array(
	    'taxonomy' => $terms,
	    'hide_empty' => true,
	) );
	
	$output = "<div class='auto-col-3'>";
	
	
	foreach ( $tax_terms as $theterm ) {
		$output .= '<h3><a name="'.$theterm->slug.'"></a>' . $theterm->name . '</h3> <ul>';
	
	    $args = array(
		    'posts_per_page' => $count,
		    'post_type' => $posts,
			'tribeHideRecurrence' => true,
			'start_date'     => 'today',
		    'tax_query' => array(
				array(
					'taxonomy' => $terms,
					'field' => 'slug',
					'terms' => $theterm->slug,
				),
				array(
				    'taxonomy' => "semester",
				    'field'		=> 'slug',
				    'terms'		=> $current_semester->slug,
				    'operator'	=> "IN",
				),

			)
		
	     );
	  
	    $tax_terms_posts = tribe_get_events($args);

	    
	    foreach ( $tax_terms_posts as $post ) {
	        $output .= '<li><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
	    }
	$output .= '</ul>';
	}
	
	wp_reset_postdata();
	$output .= '</div>';
	return $output;
}


add_shortcode( 'list_grouped_posts', 'list_posts_by_term' );

Initial Tags
wordpress

Initial Language
PHP

Initial URL