Moving WordPress php code for ordering events by date and grouping by category into Timber Twig templates
<p>I'm moving a standard WordPress theme into Twig templates using the Timber plugin.</p>

<p>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:</p>

<pre><code>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
</code></pre>

<p>I had this working without using twig with the following code in my original template:</p>

<pre><code>$today = current_time('Ymd');
$args = array(
    'orderby' =&gt; 'post_title',
    'category_name'  =&gt; 'events',
     'exclude' =&gt; 28
);
$cats = get_categories( $args );

foreach( $cats as $cat ) :
    $args = array(
        'post_type' =&gt; 'cpt_shows',
        'meta_query' =&gt; array(
            array(
                'key' =&gt; 'date',
                'compare' =&gt; '&gt;=',
                'value' =&gt; $today,
                'type' =&gt; 'NUMERIC,'
            )
        ),
        'meta_key' =&gt; 'date',
        'orderby' =&gt; 'meta_value',
        'order' =&gt; 'ASC',
        'posts_per_page' =&gt; -1,
        'category__in'        =&gt; array( $cat-&gt;term_id ),
        'ignore_sticky_posts' =&gt; 1
    );
    $query = new WP_Query( $args );

    if ( $query-&gt;have_posts() ) {
        echo '&lt;h2&gt;&lt;a href="' . get_category_link( $cat-&gt;term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat-&gt;name ) . '" ' . '&gt;' . $cat-&gt;name.'&lt;/a&gt;&lt;/h2&gt; ';
        while( $query-&gt;have_posts() ) : $query-&gt;the_post();
            ?&gt;
            &lt;a href="&lt;?php the_permalink();?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;br&gt;
            &lt;?php
        endwhile;
        wp_reset_postdata();
    }
endforeach;
</code></pre>

<p>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 </p>

<pre><code>    {% for cat in categories %}
        {% for post in loopSetupinContext %}
</code></pre>

<p>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.</p>

<p>Any help is greatly appreciated.</p>