I have a custom post and custom taxonomy my taxonomy is called "department"

i have my loop showing my custom post, I am trying to also show all the departments (custom taxonomy) that my custom post falls under.

My different post are all set different departments however I am only seeing the same department on each post, not the department related to the post.

Am i missing something to show this ?

                    $loop = new WP_Query(array('post_type' => 'career', 'posts_per_page' => 10));
                    $taxonomy = get_queried_object();
                    while ($loop->have_posts()) :
                        $loop->the_post(); ?>
                            <p><?php echo $term->name; ?></p>
                            echo 'Department', $taxonomy->name;
                            $link = get_field('job_link');
                            if ($link):
                                $link_url = $link['url'];
                                $link_title = $link['title'];
                                $link_target = $link['target'] ? $link['target'] : '_self';

                                <a class="button" href="<?php echo esc_url($link_url); ?>"
                                   target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
                            <?php endif;
                            <p>Location: <?php the_field('location'); ?></p>
                    <?php endwhile; ?>

1 Answer 1


You can use get_the_terms() to retrieve terms from a custom taxonomy which are attached to a post.

In your Loop you could do the following to retrieve the terms,

$departments = get_the_terms( get_the_ID(), 'department' );

This gives you an array of WP_Terms. If for example, you want to use just the first one you could do it like this.

echo ($departments && is_array($departments) ? $departments[0]->name : '';
  • thank you, then just echo $department
    – Beep
    Apr 13 at 17:38
  • 2
    @Beep Yes and no, get_the_terms() gives you an array of WP_Term objects so you'll need to decide how you want to handle them - loop, use just the first one or something else. I'll add another example to my answer. Apr 13 at 17:43
  • thank you very much
    – Beep
    Apr 13 at 17:50

Your Answer

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

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