WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For a post we need to maintain a list of "related posts" (links to other posts), and this list should be visible to a user.

What is the easiest way to do this?

share|improve this question

Or, you can place this code in your active theme's functions.php:

// First, backup the default $postdata 
$backup = $post; 
// Now, override the default 
$tags = wp_get_post_tags($post->ID); 
// Now, open the if ( $tags ) statement 
if ($tags) { 
    $tag_ids = array(); 
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;  
    // Now, setup your custom query args                     
        'tag__in' => $tag_ids, 
        'post__not_in' => array($post->ID), 
        'showposts'=>10, // Number of related posts that will be shown.                                                                                                             
    // Now, perform your custom query 
    $my_query = new wp_query($args);  
    // Next, open your custom query IF statement 
    if( $my_query->have_posts() ) { 
        // We have posts, so let's output the opening-containing markup 
        <div class="relatedposts">

                // Now, open your custom query WHILE statement 
                while ($my_query->have_posts()) {                                                                
    <ul><li>      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul>
                    // Now, close the while $my_query->have_posts() statement 
                // Now, output your closing-containing HTML content 
    // Now, close the if $my_query->have_posts() statement 
    // and open the ELSE statement, for your no-posts content 
    } else { 
        <?php // If there are no related posts ?>
        <p class="noposts">FURTHER READING: <span>You may find more posts like this by searching the site...</span></p>
    // Now, close the ELSE statement 
// Now, close the if ( $tags ) statement 
// Now, reset the default query 
$post = $backup; 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.