How do I display related post thumbnails? This is the code that I use:

function related_posts_shortcode( $atts ) {
    'limit' => '3',
), $atts));

global $wpdb, $post, $table_prefix;

if ($post->ID) {
    $retval = '<div class="related-posts-inline"><p class="related-title-inline">BACA JUGA</p><ul class="related-list-inline">';
    // Get tags
    $tags = wp_get_post_tags($post->ID);
    $tagsarray = array();
    foreach ($tags as $tag) {
        $tagsarray[] = $tag->term_id;
    $tagslist = implode(',', $tagsarray);

    // Do the query
    $q = "SELECT p.*, count(tr.object_id) as count
        FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id  = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
            AND p.post_status = 'publish'
            AND p.post_date_gmt < NOW()
        GROUP BY tr.object_id
        ORDER BY count DESC, p.post_date_gmt DESC
        LIMIT $limit;";

    $related = $wpdb->get_results($q);
    if ( $related ) {
        foreach($related as $r) {
            $retval .= '
<li><a class="link-related" href="'.get_permalink($r->ID).'" title="'.wptexturize($r->post_title).'">'.wptexturize($r->post_title).'</a></li>';
    } else {
        $retval .= '
<li>No related posts found</li>';
    $retval .= '</ul></div>';
    return $retval;
add_shortcode('related_posts', 'related_posts_shortcode');

1 Answer 1


I'm fetching the attachment ID by get_post_thumbnail_id function which is taking the post ID. After getting the attachment ID of that post, I am passing this attachment ID to wp_get_attachment_image_src function and getting the image details.

So I am rewriting your foreach loop like this way

foreach($related as $r) {
    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($r->ID), 'thumbnail', true );
    $img= '';

    if( is_array( $thumbnail ) ) {
        $img = sprintf('<img src="%s" width="%s" height="%s" alt="%s"> ', $thumbnail[0], $thumbnail[1], $thumbnail[2], $r->post_title);

    $retval .= '<li><a class="link-related" href="'.get_permalink($r->ID).'" title="'.wptexturize($r->post_title).'">'. $img . wptexturize($r->post_title).'</a></li>';
  • hi @chinmoy it's not working.
    – R.M. Reza
    Jan 30, 2019 at 11:20
  • post have featured image? Jan 30, 2019 at 12:35
  • yes, but now it works. I use this code: '.get_the_post_thumbnail($dtwd_related->ID, array(100,75)).' and it works fine. I have one more question @chinmoy , what if the post from the category is the top priority? if there are no posts in the category then take it from the tag.
    – R.M. Reza
    Jan 30, 2019 at 13:27
  • I got this link. You will deeply check the and merge it. Currently your code is picking the post from tags only. Jan 30, 2019 at 13:34
  • Thank you @chinmoy, I failed to understand. I currently don't use the code anymore. I use the code here: link . I only studied PHP Wordpress for a few days, I only relied on logic, I couldn't understand it.
    – R.M. Reza
    Jan 30, 2019 at 13:45

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.