Sort order by slug for looped child terms of custom taxonomy

I have page that displays multiple carousels that contains the posts for each child term of parent taxonomy: projects for the custom taxonomy: portfolio-categories for CPT: portfolios.

When Portfolio posts are added with a new portfolio category under the parent category “projects”, it automatically adds a new carousel to the page for that child term.

It’s all doing exactly what I need it to, however I want to sort the carousels alphabetically by slug. I have been at this for days and and I just can’t find a solution that I can implement correctly with my experience.

I have also tried using various custom taxonomy sort order plugins however they have no effect on this page.

Any assistance to do either of the following would be greatly appreciated:

Get this code working with a sorting plugin, or
Edit the code to show the carousels in order of the child term slugs

See it at this link http://shakespeare-design.com.au/portfolio-gallery/

$post_type = ‘portfolio’;
$posts_per_page = 1;

$taxonomies = get_object_taxonomies( array( ‘post_type’ => $post_type, ‘posts_per_page’=> $posts_per_page ) );

foreach( $taxonomies as $taxonomy ) :
// Gets every “category” (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
$term_id = 61;
$taxonomy_name = ‘portfolio_category’;
$term_children = get_term_children( $term_id, $taxonomy_name );

foreach ( $term_children as $child ) :
$term = get_term_by( ‘id’, $child, $taxonomy_name );
?>
<div class=”container”>
<div class=”owl-carousel owl-left”>
<?php //echo $term->name; ?>

<?php
$args = array(
‘post_type’ => $post_type,
‘posts_per_page’ => -1, //show all posts
‘tax_query’ => array(
array(
‘taxonomy’ => $taxonomy,
‘field’ => ‘slug’,
‘terms’ => $term->slug,
)
)

);
$posts = new WP_Query($args);

if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<div class=”project-carousel-slide” data-caption-1=”<?php echo $term->name; ?>” data-caption-2=”<?php
if($caption = get_post(get_post_thumbnail_id())->post_excerpt || ” !== get_post()->post_content) :
echo get_the_title();
else :
endif;
?>” data-caption-3=”<?php
if($caption = get_post(get_post_thumbnail_id())->post_excerpt):
echo $caption;
elseif( ” !== get_post()->post_content ) :
echo the_content();
else :
echo $term->description;
endif;
?>”>
<?php
$tn_id = get_post_thumbnail_id( $post->ID );
$img = wp_get_attachment_image_src( $tn_id, $size );
$width = $img[1];
$height = $img[2];
if (has_post_thumbnail($post->ID )){
if ($width > $height){ ?>
<?php the_post_thumbnail(‘owl-landscape’, array( ‘class’ => “attachment-owl-landscape size-owl-landscape landscape”) );
}else{ ?>
<?php the_post_thumbnail(‘owl-portrait’, array( ‘class’ => “attachment-owl-portrait size-owl-portrait portrait”) );
}
}
?>
</div><!– .project-carousel-slide –>

<?php endwhile; endif; ?>

</div><!– .owl-carousel –>
</div><!– .container–>
<?php endforeach; ?>
<?php endforeach; ?>

Read more here:: Sort order by slug for looped child terms of custom taxonomy

Leave a Reply

Your email address will not be published. Required fields are marked *