admin管理员组

文章数量:1315831

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

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

Share Improve this question asked Nov 17, 2020 at 10:50 user1348927user1348927 111 silver badge6 bronze badges 3
  • 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 Commented 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. – user1348927 Commented 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 Commented Nov 17, 2020 at 11:29
Add a comment  | 

1 Answer 1

Reset to default 0

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; ?>
</span></td>

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

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

}
 ?>
</span></td>

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

本文标签: categoriesOrdering terms whilst in loop