I'm moving a standard WordPress theme into Twig templates using the Timber plugin.
My goal is to list a custom post type called cpt_shows (events) by date but have them listed and grouped by artist. For example:
Artist A
event - April 1
event - May 1
event - June 1
Artist B
event - April 1
event - May 1
event - June 1
Artist C
event - April 1
event - May 1
event - June 1
I had this working without using twig with the following code in my original template:
$today = current_time('Ymd');
$args = array(
'orderby' => 'post_title',
'category_name' => 'events',
'exclude' => 28
);
$cats = get_categories( $args );
foreach( $cats as $cat ) :
$args = array(
'post_type' => 'cpt_shows',
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $today,
'type' => 'NUMERIC,'
)
),
'meta_key' => 'date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'category__in' => array( $cat->term_id ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></h2> ';
while( $query->have_posts() ) : $query->the_post();
?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a><br>
<?php
endwhile;
wp_reset_postdata();
}
endforeach;
What I can't wrap my head around is how to move this into my Twig template because i have templating in my logic code, specifically the 'category__in' => array( $cat->term_id ), is being set in the loop. I've tried things in Twig like
{% for cat in categories %}
{% for post in loopSetupinContext %}
without success. Is there a better way to do this intially? In shortL i have a solution for my output but i'm unsure how to move it into Timber/Twig.
Any help is greatly appreciated.