Using multiple taxonomies to sort Custom Posts

I have a post type that includes a taxonomy for (1) location and (2) a day of the week.

I’m using the first taxonomy to sort the posts into groups. Here’s the loop I’m using:

Read More
<?php 
$terms = get_terms('cell-locations');
$argv = array(
                'orderby'       =>  'by_term',
                'hide_empty'    => false
                );
foreach ($terms as $term) {
  $wpq = array ('taxonomy'=>'cell-locations','term'=>$term->slug);
  $myquery = new WP_Query ($wpq);
  $article_count = $myquery->post_count;
  echo '<div class="accordionButton">';
  echo "<h2 class="cellHeader" id="".$term->slug."">";
  echo $term->name;
  echo "</h2>";
  echo '</div>';
  echo '<div class="accordionContent">';
  if ($article_count) {
     echo "<ul class='cell_list'>";
     while ($myquery->have_posts()) : $myquery->the_post();?>
                            <li class="cell-item">
                                <ul class="cell-list">
                                    <li><?php $terms_as_text = get_the_term_list( $post->ID, 'cell-days', '', ', ', '' ) ;
                                        echo strip_tags($terms_as_text);
                                    ?> </li>
                                    <li> <? 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="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;
     echo "</ul>";
  }
  echo '</div>';
}
?>

This gives me a nice accordion-style layout based on the taxonomy term, “cells-location.” This works all fine, except I now want to sort the posts within each location according to the other taxonomy, “cells-days.” I’ve used a plugin to give them a sort order (http://wordpress.org/extend/plugins/taxonomy-terms-order/). The api for the plugin offers the following query arguments to call the posts in order:

 $argv = array(
                'orderby'       =>  'term_order',
                'hide_empty'    => false
                );
get_terms('category', $argv);

I’m having trouble creating this second loop within the first loop. Any thoughts or suggestions?

Related posts

Leave a Reply

1 comment

  1. 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="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; ?>