Last updated on September 18, 2023

Display Related Posts Based on Tags

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Increase engagement with related posts.

To display related posts based on tags in WordPress, you can use the get_posts() function along with the wp_get_post_tags() function. Here’s an example code snippet that retrieves related posts based on tags:

function wpsnippets_display_related_posts() {
    global $post;

    // Get the tags of the current post
    $tags = wp_get_post_tags( $post->ID );

    if ( $tags ) {
        $tag_ids = array();
        foreach ( $tags as $tag ) {
            $tag_ids[] = $tag->term_id;
        }

        // Query related posts based on tags
        $args = array(
            'tag__in' => $tag_ids,
            'post__not_in' => array( $post->ID ),
            'posts_per_page' => 5,
            'ignore_sticky_posts' => 1
        );

        $related_posts = get_posts( $args );

        // Display the related posts
        if ( $related_posts ) {
            echo '<ul>';
            foreach ( $related_posts as $related_post ) {
                echo '<li><a href="' . get_permalink( $related_post->ID ) . '">' . get_the_title( $related_post->ID ) . '</a></li>';
            }
            echo '</ul>';
        }
    }
}

To use this code snippet, you can simply call the wpsnippets_display_related_posts() function within your WordPress theme template files, such as single.php or content.php, where you want to display the related posts.

This code snippet retrieves the tags of the current post using the wp_get_post_tags() function. It then constructs a query to retrieve related posts based on those tags using the get_posts() function. The retrieved related posts are then displayed in an unordered list using a foreach loop.

Examples

Example 1: Display related posts based on tags

This use case demonstrates how to display related posts on a WordPress website based on the tags assigned to each post. The code example below retrieves the tags of the current post, queries for other posts that have at least one matching tag, and displays them as related posts.

<?php
function wpsnippets_display_related_posts() {
    $tags = wp_get_post_tags( get_the_ID() );

    if ( $tags ) {
        $tag_ids = array();
        foreach ( $tags as $tag ) {
            $tag_ids[] = $tag->term_id;
        }

        $args = array(
            'tag__in' => $tag_ids,
            'post__not_in' => array( get_the_ID() ),
            'posts_per_page' => 3,
            'ignore_sticky_posts' => 1,
        );

        $related_posts = new WP_Query( $args );

        if ( $related_posts->have_posts() ) {
            echo '<ul>';
            while ( $related_posts->have_posts() ) {
                $related_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
            wp_reset_postdata();
        }
    }
}
?>

The code first retrieves the tags assigned to the current post using the wp_get_post_tags() function. It then constructs a query to find other posts that have at least one matching tag, excluding the current post itself. The WP_Query class is used to execute the query and retrieve the related posts. Finally, the related posts are displayed as an unordered list.

Example 2: Display related posts with custom post type

This use case demonstrates how to display related posts based on tags for a custom post type in WordPress. The code example below is similar to the previous example, but it includes an additional argument in the query to specify the custom post type.

<?php
function wpsnippets_display_related_posts_custom_post_type() {
    $tags = wp_get_post_tags( get_the_ID() );

    if ( $tags ) {
        $tag_ids = array();
        foreach ( $tags as $tag ) {
            $tag_ids[] = $tag->term_id;
        }

        $args = array(
            'tag__in' => $tag_ids,
            'post__not_in' => array( get_the_ID() ),
            'posts_per_page' => 3,
            'ignore_sticky_posts' => 1,
            'post_type' => 'my_custom_post_type',
        );

        $related_posts = new WP_Query( $args );

        if ( $related_posts->have_posts() ) {
            echo '<ul>';
            while ( $related_posts->have_posts() ) {
                $related_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
            wp_reset_postdata();
        }
    }
}
?>

The code is similar to the previous example, but it includes the 'post_type' => 'my_custom_post_type' argument in the query to specify the custom post type. Replace 'my_custom_post_type' with the actual name of your custom post type.

Example 3: Display related posts with custom number of posts

This use case demonstrates how to display a custom number of related posts based on tags. The code example below is similar to the first example, but it includes an additional argument in the query to specify the number of related posts to display.

<?php
function wpsnippets_display_related_posts_custom_number() {
    $tags = wp_get_post_tags( get_the_ID() );

    if ( $tags ) {
        $tag_ids = array();
        foreach ( $tags as $tag ) {
            $tag_ids[] = $tag->term_id;
        }

        $args = array(
            'tag__in' => $tag_ids,
            'post__not_in' => array( get_the_ID() ),
            'posts_per_page' => 5,
            'ignore_sticky_posts' => 1,
        );

        $related_posts = new WP_Query( $args );

        if ( $related_posts->have_posts() ) {
            echo '<ul>';
            while ( $related_posts->have_posts() ) {
                $related_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
            wp_reset_postdata();
        }
    }
}
?>

The code is similar to the first example, but it includes the 'posts_per_page' => 5 argument in the query to specify the number of related posts to display. Replace 5 with the desired number of related posts to show.

Last updated on September 18, 2023. Originally posted on September 18, 2023.

Leave a Reply

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