PHP: Use WordPress blog tags to find related posts

Posted in WordPress on 1 February 2016

This post has been archived

The content of this post has not been updated since 2015, and may be out of date. Extra care should be taken with any code provided.

A simple script that can be used to pull related-type posts for a WordPress blog post. The related aspect is based on the fist tag assigned to the post. The script will return at most 5 other posts from the site that also include the first tag.

<?php

$tags = wp_get_post_tags($post->ID);

if ($tags) {
    
    $first_tag = $tags[0]->term_id;
    
    $select = array(
       'tag__in'          => array($tags[0]->term_id),
       'post__not_in'     => array($post->ID),
       'posts_per_page'   => 5,
       'caller_get_posts' => 1
    );
    
    $tag = get_tag($first_tag);
   
    $tag_query = new WP_Query($select);
   
    if( $tag_query->have_posts() ) {
        echo "<ul>";
        while ($tag_query->have_posts()) : $tag_query->the_post(); ?>
            <li>
                <a href="<?php the_permalink() ?>">
                   <?php the_title(); ?>
                </a>
            </li>

        <?php endwhile;
        echo "</ul>";
    }
    
    wp_reset_query();
}

?>

WordPress developer

in Belfast, Northern Ireland

I’ve been developing WordPress websites for 10 years and am always looking for the next exciting project from personal blog, community portal or business website. Why not get in touch to see if I can help you with yours.

Contact me today!

Related WordPress Posts

October 2024

What's going on between WordPress and WP Engine?

The disagreement between WordPress and WP Engine has sparked considerable debate within the WordPress community and could have important implications for users of the WordPress...

Continue reading

September 2024

Combining Laravel with WordPress

Combining Laravel with WordPress offers a unique and powerful approach to web development, blending the strengths of both platforms to create highly efficient, flexible, and...

Continue reading

September 2024

Switching from WordPress to ClassicPress

Switching a WordPress website over to ClassicPress can be done smoothly with minimal impact if approached carefully. The process involves a few key steps to...

Continue reading

July 2024

Bitnami WordPress on AWS Lightsail

The Bitnami package for WordPress provides a quick one-click install solution for WordPress. Once installed you'll most likely need to replace the default install with...

Continue reading

April 2024

ClassicPress vs. WordPress

ClassicPress is an open-source content management system (CMS) that originated as a fork of WordPress in 2018. It was developed as a response to the...

Continue reading

November 2023

Creating a brand-promoting brochure website with WordPress

In today's digital age, establishing an online presence is crucial for businesses looking to promote their brand effectively. One powerful tool for achieving this is...

Continue reading

More WordPress Posts