Skip Navigation

[Resolved] Ordering terms whilst in loop

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 1 voice.

Last updated by chantelleL-2 4 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1847175

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 Hierachy is

Science (parent)

-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

#1847301

My issue is resolved now. Thank you!