I have a custom post type called email_block and have a custom taxonomy called block_type. I need to loop through all the email blocks find out what custom taxonomy (block_type) they have. I know how get all the email block custom post types, it's finding the what block_type they belong to is what I'm struggling with.

This is the code I have so far. I'm using a relationship field from advanced custom fields to filter what email blocks I want to display.


$posts = get_field('block_selector');

if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <span>Custom field from $post: <?php the_field('author'); ?></span>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly 
    endif; ?>

2 Answers 2


You mean get_the_terms()?

    $terms = get_the_terms( $post->ID, 'block_type' ); 
    foreach($terms as $term) {
      echo $term->name;

Or have I simplified this too much?

  • No that was exactly what I was looking for! Thanks :)
    – imz
    Jul 2, 2014 at 22:12
  • Is there a way to know the heirachy of the custom taxonomies and pass that to the output?
    – dubbs
    Nov 19, 2022 at 7:47

this is best way to do it

            $taxonomy = 'movies-category';
            $terms = get_object_term_cache( $post->ID, $taxonomy );
            $output = '';
            foreach($terms as $term) {
                    $output .= ' | ';
                    $output .= '<span class="cat"><a href="'. esc_url( get_term_link( $term )). '">'.$term->name.'</a></span>';
            echo $output;

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.