Ordering Child Term Headings with list of posts in Custom Tax archive
-
Hi,
I am building a business listings site using custom post type (goto_listing) and custom taxonomy (business-categories).
I have set up templates for the taxonomy archive pages for each (parent) term (e.g. taxonomy-business-categories-fabrication.php)
On these archives I want:
Under the parent term title (eg Fabrication) I want the child terms as h3 headings and the listings (custom posts) underneath.
I have achieved this but currently only the post titles are in alphabetical order. Their headings (child terms) appear to be ordered by the date (for some reason).
Here is the code that gets the current parent term it’s child terms and the related posts:
<?php $term_slug = get_query_var( 'term' ); $taxonomyName = get_query_var( 'taxonomy' ); $current_term = get_term_by( 'slug', $term_slug, $taxonomyName ); $termchildren = get_term_children( $current_term->term_id, $taxonomyName ); foreach ($termchildren as $child) { $term = get_term_by( 'id', $child, $taxonomyName ); $wpq = array ( 'post_type' => 'goto_listing', 'taxonomy'=>$taxonomyName, 'term'=>$term->slug, 'posts_per_page'=>'20', 'orderby'=>'title', 'order'=>'ASC' ); $query = new WP_Query ($wpq); echo "<h3>$term->name</h3>"; ?> <ul class="goto-leaders"> <?php if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?> <li><span><?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></span></li> <?php endwhile; wp_reset_postdata(); ?> </ul>
The result is:
Child Term B
A Business
B Business
C BusinessChild Term A
A Business
B Business
C BusinessChild Term C
A Business
B Business
C BusinessI need it to be:
Child Term A
A Business
B Business
C BusinessChild Term B
A Business
B Business
C BusinessChild Term C
A Business
B Business
C BusinessBasically I need an orderby ‘term name’ order ASC but after many many many hours I have been unsuccessful.
It seems like such a simple task, maybe I’m overcomplicating it?
Thanks for any help you can give.
Cheers,
Tracy
- The topic ‘Ordering Child Term Headings with list of posts in Custom Tax archive’ is closed to new replies.