Ordering terms whilst in loop

Question

I have a page template that shows all the "posts" within two custom taxonomies, the posts are displayed in a table

$type = $_GET['type'];
$category = $_GET['category'];
    args = array(
        'post-type' => 'literature',
        'posts_per_page' => -1,
        'tax_query' => array(
            'relation' => 'AND',
                array(
                 'taxonomy' => 'type',
                 'field' => 'slug',
                 'terms' => $type
            ),
            array(
                'taxonomy' => 'custom-category',
                'field' => 'slug',
                'terms' => $category
            )
    )
    );
    $query = new WP_Query($args);
    
    if ($query->have_posts()) :
        
                
            while ( $query->have_posts() ) : $query->the_post();

The I pull some custom fields etc

Now I need the terms that are assigned to the post

<?php $terms = get_the_terms( $post, 'custom-category' ); foreach($terms as $term) {
?><td class="custom-cat"> <span class="term">
<?php echo $term->name; ?>
</span></td><?php
}

?>

Which works fine and on the front end I get

<td>Science</td> <td>Space</td>
<td>Earthquakes</td> <td>Science</td>
<td>Science</td> <td>Volcanoes</td>
<td>Science</td> <td>Space</td>
<td>Science</td> <td>Volcanoes</td>
<td>Earthquakes</td> <td>Science</td>

However, On the second and last result you can see it puts Earthquakes before Science?

My hierarchy is

Science (parent term)

-Earthquakes (child)

-Space (child)

-Volcanoes (child)

I am assuming this is because the default order is A-Z? If I could make this order by term_id then it should work?

How could I go about this please

0
user1348927 2 months 0 Answers 11 views 0

Leave an answer