I have taken a few stabs at this and have hit a road block. I will try to be as descriptive as possible...

  1. I have a Custom Post Type: Bars
  2. I have a Custom Taxonomy associated with Bars called: Location
  3. Location has several cities associated with it. e.g. Dallas, Atlanta, Philadelphia, etc.

I have a created a shortcode that currently works with the exception that I cannot figure out how to grab the "terms" associated with "location" and display them in the Recent Post list on the front-end. Only the Thumbnail, Title, and Date are displaying.

Currently I am using wp_get_recent_posts but after some reading, should I be using that at all? Or should I be using WP_Query?

Here is my code:

function recent_cpt_list_display( $atts ) {
  $atts = shortcode_atts( array(
    'cpt_type' => 'bars',
    'show_posts' => 5,
    'cpt_cat' => 'location',
  ), $atts, 'cpt-recent-posts' );

  global $post;

  $cpt_type = $atts['cpt_type'];
  $show_posts = $atts['show_posts'];
  $cpt_cat = $atts['cpt_cat'];

  $cpt_posts = wp_get_recent_posts( array(
    'post_type' => $cpt_type,
    'orderby' => 'date',
    'order' => 'ASC',
    'numberposts' => $show_posts

  if ( ! empty( $cpt_posts ) && ! is_wp_error( $cpt_posts ) ) {
    $output = '<ul class="cpt-recent-posts">';

    foreach( $cpt_posts as $cpt_post ){
        $output .= '<li>';
        $output .= '<div class="cpt-recent-posts-thumb">' . get_the_post_thumbnail( $cpt_post['ID'], 'thumbnail' ) . '</div>';
        $output .= '<div class="cpt-recent-meta">';
        $output .= '<a href="' . get_permalink( $cpt_post["ID"] ) . '" title="' . esc_attr( $cpt_post["post_title"] ) . '" >' . $cpt_post["post_title"].'</a>';
        $output .= '<div class="cpt-meta">' . NEED TO OUTPUT LOCATION HERE . '</div>';
        $output .= '<div class="cpt-post-date">' . get_the_time( get_option( 'date_format' ), $post->ID ) . '</div>';
        $output .= '</div></li>';
      $output .= '<ul>';
  return $output;
add_shortcode( 'cpt-recent-posts', 'recent_cpt_list_display' );

Any insight as to do this better, I am all ears as I am still learning. I am trying to rely less on using plugins for everything and really want to code as much as possible.

Thanks in advance.

1 Answer 1


Use get_the_terms to fetch terms for a specific post:

$terms = get_the_terms( $cpt_post['ID'], 'location' );

if ( $terms && ! is_wp_error( $terms ) ){
    $output .= '<div class="cpt-meta">';
    foreach ( $terms as $term ) {
        $output .= $term->name . ' ';
    $output .= '</div>';
  • How would I output that in the order within the existing foreach loop?
    – Jason Ryan
    Commented Jul 1, 2017 at 15:47
  • The above code would basically replace the whole line where you have NEED TO OUTPUT LOCATION HERE right now, so it would be inside your existing foreach loop.
    – Milo
    Commented Jul 1, 2017 at 19:56
  • unfortunately I had tried something like that previously but it only dumps out the "location" of the post the shortcode was placed on, not the location of each "Recent Post".
    – Jason Ryan
    Commented Jul 2, 2017 at 0:24
  • ah, sorry, see edit above. you were likely not passing the correct post ID. $post->ID refers to the post that contains the shortcode, you need to pass $cpt_post['ID'], which is the ID of each post in your foreach loop.
    – Milo
    Commented Jul 2, 2017 at 0:28
  • thank you, this definitely works. However, my other question... would using WP_Query and passing $args be a better solution or is this fine? It seems like a lot of people have different methods of approaching something like this, and with me just starting out learning, I am trying to find out what is "best practice". Thanks again.
    – Jason Ryan
    Commented Jul 2, 2017 at 0:34

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.