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:
<?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?
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:Therefore, we can rewrite your
get_terms()
and$argv
for thecells-days
taxonomy like so:Then you can create another
foreach
loop inside of your first<li>
element inul.cell-list
(At least that seems to be where you’re attempting to loop throughcells-days
):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: