October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Add Related Posts With Thumbnails in WordPress Without a Plugin

Use WordPress’s built-in query and thumbnail functions to add a related-post section to a classic theme without a dedicated plugin.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add a related-post section to a classic WordPress theme by defining a relevance rule—such as shared categories—then running a secondary query and linking each result’s title and featured image to its post. Use get_posts() or a separate WP_Query, not query_posts(); the latter replaces the main query rather than serving as a safe secondary-listing tool.

Before you add the related-post section

This pattern is for a classic theme with editable PHP templates, typically the single-post template. Back up the theme and make changes in a child theme where possible, so an update to the parent theme does not overwrite them. The exact template and styling depend on the active theme.

Confirm featured-image support

The theme needs to declare add_theme_support( 'post-thumbnails' ) for WordPress’s featured-image interface to appear. WordPress recommends registering theme support in a setup callback on after_setup_theme, before init. See the add_theme_support() reference and the Featured Images & Post Thumbnails handbook page.

If you control the theme, a typical setup function looks like this:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function mytheme_setup() {
    add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'mytheme_setup' );

Do not add this blindly if the theme already declares support; first check its setup code.

Choose what “related” means

WordPress does not decide which posts are editorially related. Choose a rule that matches how the site is organized. A shared category is a straightforward starting point; shared tags or a custom taxonomy can produce a narrower set. Query functions retrieve posts that match criteria, but the relevance rule remains your choice. See the get_posts() reference for available query arguments.

Rule Useful when Trade-off
Shared category Categories describe broad subjects and posts are consistently assigned to them. Broad categories can return loosely related posts.
Shared tag Tags are applied consistently to identify specific topics. Inconsistent or overly broad tagging can weaken relevance.
Custom taxonomy The site has a purpose-built classification for a particular content relationship. It requires that taxonomy to be registered and used consistently.

Add a related-post list with get_posts()

For a compact list, get_posts() returns an array of matching posts without replacing the main query. In the following example, related posts share at least one category with the current post. Place the section in the single-post template where it should appear, commonly after the post content. Adjust the count and image size to suit the design.

<?php
$current_post_id = get_the_ID();
$category_ids = wp_get_post_categories( $current_post_id );

if ( $category_ids ) {
    $related_posts = get_posts( array(
        'post_type'           => 'post',
        'post_status'         => 'publish',
        'posts_per_page'      => 4,
        'post__not_in'        => array( $current_post_id ),
        'category__in'        => $category_ids,
        'ignore_sticky_posts' => true,
    ) );

    if ( $related_posts ) {
        echo '<section class="related-posts" aria-labelledby="related-posts-title">';
        echo '<h2 id="related-posts-title">Related posts</h2>';
        echo '<ul>';

        foreach ( $related_posts as $related_post ) {
            $post_thumbnail = get_the_post_thumbnail(
                $related_post->ID,
                'medium'
            );

            if ( ! $post_thumbnail ) {
                continue;
            }

            echo '<li>';
            echo '<a href="' . esc_url( get_permalink( $related_post->ID ) ) . '">';
            echo $post_thumbnail;
            echo '<span>' . esc_html( get_the_title( $related_post->ID ) ) . '</span>';
            echo '</a>';
            echo '</li>';
        }

        echo '</ul>';
        echo '</section>';
    }
}
?>

This example excludes the current post and limits the query to published standard posts. It also omits results without a featured image, so every displayed item has a thumbnail. If you want image-less posts included, remove the if ( ! $post_thumbnail ) check and render a designed fallback instead. The WordPress reference for get_the_post_thumbnail() documents output for a specified post and image size.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the query style that fits the template

Use get_posts() when a simple array and explicit rendering are convenient. A separate WP_Query is also appropriate, particularly when you want to use WordPress’s loop methods for template output. WordPress explicitly recommends these two approaches for secondary listings, including related posts: query_posts() – Function.

With a custom WP_Query, keep the secondary loop separate from the main post loop and restore post data after it finishes:

<?php
$related_query = new WP_Query( array(
    'post_type'           => 'post',
    'post_status'         => 'publish',
    'posts_per_page'      => 4,
    'post__not_in'        => array( get_the_ID() ),
    'category__in'        => wp_get_post_categories( get_the_ID() ),
    'ignore_sticky_posts' => true,
) );

if ( $related_query->have_posts() ) :
    echo '<section class="related-posts">';
    echo '<h2>Related posts</h2>';
    echo '<ul>';

    while ( $related_query->have_posts() ) :
        $related_query->the_post();

        if ( ! has_post_thumbnail() ) {
            continue;
        }
        ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail( 'medium' ); ?>
                <span><?php the_title(); ?></span>
            </a>
        </li>
        <?php
    endwhile;

    echo '</ul>';
    echo '</section>';
endif;

wp_reset_postdata();
?>

Do not use query_posts() to create this feature. It alters the main query and can interfere with the page’s existing loop and pagination; a secondary listing should leave that query intact.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Pick an image size and handle missing thumbnails

Use a registered image size that fits the card rather than serving a full-size image by default. WordPress documents these default maximum dimensions: thumbnail 150 × 150 pixels, medium 300 × 300 pixels, medium_large up to 768 pixels wide with no height limit, and large 1024 × 1024 pixels. These are defaults and can be changed in Media settings. The Featured Images & Post Thumbnails documentation describes registered sizes and thumbnail output.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a consistent card grid, decide whether image-less posts should be omitted or shown with a fallback image. The examples omit them. If you register a custom image size for the cards, note that doing so does not automatically generate that size for existing featured images; regenerate image derivatives using an appropriate site workflow if needed. See set_post_thumbnail_size() for the standard thumbnail-size function and the handbook page for custom image sizes.

Style and verify the section

Add CSS in the theme’s stylesheet or your site’s established styling location. The markup uses a related-posts section and list so it can be styled without changing the query logic. For example, a grid can be created with CSS on .related-posts ul, while image dimensions and cropping can be controlled with .related-posts img. Avoid hard-coding dimensions that distort the chosen image ratio.

  • Open posts that have several matching categories, only one match, and no matching posts; confirm the section behaves sensibly in each case.
  • Check that the current post never appears in its own list.
  • Test related posts both with and without featured images, according to the fallback policy you chose.
  • Confirm titles and images both link to the correct post and that the image size suits the rendered layout.
  • Verify that the theme’s main content, navigation, and pagination still behave as expected.

This is a template pattern, not a universally drop-in snippet: adapt the post type, taxonomy, placement, markup, and styling to the active theme and site structure.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.