<div class="container pt-4 pb-4 commissions">
    <div class="row-block">
        <?php
        // Liste des commissions de premier niveau
        $argsCommission = array(
            'hierarchical' => false,
            'show_option_none' => '',
            'hide_empty' => 0,
            'parent' => 0,
            'taxonomy' => 'elus-commission'
        );
        $commissions = get_categories($argsCommission);
        foreach ($commissions as $commission){
            echo '<h2>'.$commission->name.'</h2>';
            echo '<ul>';
            // Commissions de second niveau (Président(e))
            $argsPresident = array(
                'hierarchical' => 1,
                'show_option_none' => '',
                'hide_empty' => 0,
                'parent' => $commission->term_id,
                'taxonomy' => 'elus-commission'
            );
            $idPresident = array();
            $catPresident = get_categories($argsPresident);
            if(!empty($catPresident)) {
                foreach ($catPresident as $categorie) {
                    $args = array(
                        'post_type' => 'elus',
                        'orderby' => 'menu_order',
                        'order' => 'ASC',
                        'posts_per_page' => -1,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'elus-commission',
                                'field' => 'slug',
                                'terms' => $categorie->slug
                            )
                        ),
                    );
                    $queryPresident = new WP_Query($args);
                    if ($queryPresident->have_posts()) {
                        while ($queryPresident->have_posts()) {
                            $queryPresident->the_post();
                            echo '<li><strong>' . get_the_title() . '</strong></li>';
                        }
                        // Stockage de l'ID du Président pour ne pas le lister dans les memebres
                        $idPresident = array(get_the_ID());
                    }
                }
            }
            $argsMembres = array(
                'post_type' => 'elus',
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'posts_per_page' => -1,
                'post__not_in' => $idPresident,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'elus-commission',
                        'field' => 'slug',
                        'terms' => $commission->slug
                    ),
                )
            );
            $queryMembres = new WP_Query($argsMembres);
            if ($queryMembres->have_posts()) {
                while ($queryMembres->have_posts()) {
                    $queryMembres->the_post();
                    echo '<li>' . get_the_title() . '</li>';
                }
            }
            echo '</ul>';
        }
        ?>
    </div>
</div>
