Using multiple taxonomies to sort Custom Posts

Part of your trouble likely stems from the fact that you’re using the legacy version of the get_terms() function. As per the docs for this function:

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

$terms = get_terms( array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
) );

Therefore, we can rewrite your get_terms() and $argv for the cells-days taxonomy like so:

$argv = array(
    'taxonomy' => 'cells-days',
    'orderby' => 'by_term',
    'hide_empty' => false
);
$cells_days_terms = get_terms( $argv );

Then you can create another foreach loop inside of your first <li> element in ul.cell-list (At least that seems to be where you’re attempting to loop through cells-days):

<ul class="cell-list">
    <li>
        <?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
            <span class="cells-days"><?= ( $i < $i_max ? $cells_days_term->name . ', ' : $cells_days_term->name ) ?></span>
        <?php endforeach; ?>
    </li>
    <!-- The other <li> elements here -->
</ul>


On a separate note, I recommend reworking the first part to use HTML where you’re echoing, so when all is said and done your code would look like this:

<?php 
$terms = get_terms('cell-locations');
$argv = array(
    'taxonomy' => 'cells-days',
    'orderby' => 'by_term',
    'hide_empty' => false
);
$cells_days_terms = get_terms( $argv );
foreach ($terms as $term):
  $wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
  $myquery = new WP_Query ($wpq);
  $article_count = $myquery->post_count; ?>
  <div class="accordionButton">
    <h2 class="cellHeader" id="<?= $term->slug ?>">
        <?= $term->name ?>
    </h2>
  </div>
  <div class="accordionContent">
    <?php if ($article_count): ?>
      <ul class="cell_list">
        <?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
          <li class="cell-item">
            <ul class="cell-list">
              <li>
                <?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
                  <span class="cells-days"><?php echo ( $i < $i_max ? $cells_days_term->name . ', ' : $cells_days_term->name ); ?></span>
                <?php endforeach; ?>
              </li>
              <li><?php echo get_post_meta(get_the_ID(), '_cell_leader', true); ?> / <?php echo get_post_meta(get_the_ID(), '_cell_apprentice', true)?></li>
              <li>Get in touch with <a href="https://wordpress.stackexchange.com/questions/70700/mailto:<?php echo get_post_meta(get_the_ID(),"_cell_leader_email', true); ?>"><?php echo get_post_meta(get_the_ID(), '_cell_leader', true); ?></a></li>
            </ul>
          </li>
        <?php endwhile; ?>
      </ul>
    <?php endif; ?>
  </div>
<?php endforeach; ?>

Leave a Comment