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',
                 'taxonomy' => 'type',
                 'field' => 'slug',
                 'terms' => $type
                '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; ?>


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

  • Did you check the child terms in the edit screen, or did you check both the child AND the parent terms in the edit screen? I ask because parent terms include all of their child terms and their posts so checking them is not necessary, but sometimes people don't realise that's how terms work and it leads to unexpected outcomes
    – Tom J Nowell
    Nov 17, 2020 at 11:18
  • Hi Yes I did, but needed to check both as there is another page which is reliant on showing only parent terms. Nov 17, 2020 at 11:22
  • That shouldn't be a problem if it's built to account for it, by checking each parents term parents, but you would need to share that code for it to be helped with. Otherwise what you have here is ordered alphabetically, but because parent and child terms are mixed together and all hierarchy has been flattened, the structure is hidden and so it looks like it's unordered. Adjust your code to also print out the term parent ID and that'll be revealed in the output <?php echo $term->name . '- ' . $term->parent; ?>
    – Tom J Nowell
    Nov 17, 2020 at 11:29

1 Answer 1


I just realised

I already have the parent from here $category = $_GET['category'];

So I changed my code:

<td class="custom-cat"> <span class="term">
<?php echo $category; ?>

<td class="custom-sub-cat"> <span class="term">
$terms = get_the_terms( get_the_ID(), 'custom-category' );

  foreach ( $terms as $term ){
  if ( $term->parent ==! 0 ) {
    echo $term->name;


As I only have one parent and one child for each taxonomy the above works fine for me as a work around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.