When I use get_the_terms() inside a loop, the whole thing behaves very strangely. Sometimes the terms are included twice or three times and sometimes a post inside of the loop returns only false.

I have used get_the_terms() over and over for the last few years without any problems. So i'm quite confused right now.

It seems like wp_get_post_terms() and wp_get_post_categories() are getting out the correct data but I would like to know why get_the_terms() is behaving this way inside of a loop.

Can someone explain that to me?

enter image description here

Edit: Here is my code for the category badges. Btw: I'm using sage from roots.io so i'm working with blade templates. It happens on custom queries and default main queries (like: index.php, archive.php etc.)


@if (have_posts())

    @while(have_posts()) @php(the_post())

        @includeFirst(['partials.content-' . get_post_type(), 'partials.content'])





    $post_id = get_the_ID();
    $terms = get_the_terms( $post_id, 'category' );


@if($terms && ! is_wp_error( $terms ))
    <div class="badges">
        @foreach($terms as $term)
            <span class="badge">{!! $term->name !!}</span>
  • 1
    What's your code? May 25 at 9:45
  • we need to see your code and the loop, there's not enough information to figure out what's happening
    – Tom J Nowell
    May 25 at 10:10
  • @JacobPeattie Sorry my bad.. I updated the question. It's a basic normal get_the_terms() function inside of a default loop in index.php or archive.php..
    – rjanits
    May 25 at 12:13
  • This must be a caching issue since get_the_terms is a cached version of wp_get_object_terms + the caching of the template files in sage v10 . I'm trying to reach out the roots.io community for this issue. Since i got this resolved with wp_get_post_terms function.
    – rjanits
    May 25 at 14:29


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.