How to get related posts of any post type on single post page?

·

How to add custom meta title and meta description in Wordpress?

Hello, welcome to therichpost.com. In this post, How to get related posts of any post type on single post page? WordPress is the best cms. I personally like it very much and my mostly posts are related to wordpress.

Here is the working code for get related posts of any post type on single post page:
<?php

// You might need to use wp_reset_query(); 
// here if you have another query before this one

global $post;

$current_post_type = get_post_type( $post );

// The query arguments
$args = array(
    'posts_per_page' => 3,
    'order' => 'DESC',
    'orderby' => 'ID',
    'post_type' => $current_post_type,
    'post__not_in' => array( $post->ID )
);

// Create the related query
$rel_query = new WP_Query( $args );

// Check if there is any related posts
if( $rel_query->have_posts() ) : 
?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
    <ul class="group">
<?php
    // The Loop
    while ( $rel_query->have_posts() ) :
        $rel_query->the_post();
?>
        <li>
        <a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
            <article>
                <h1 class="entry-title"><?php the_title() ?></h1>
                <div class="name-date"><?php the_time('F j, Y'); ?></div>
                <div class="theExcerpt"><?php the_excerpt(); ?></div>
            </article>
        </a>
        </li>
<?php
    endwhile;
?>
    </ul><!-- .group -->
</div><!-- #related -->
<?php
endif;

// Reset the query
wp_reset_query();

?>

 If you have any query related to this then   please do comment. Thank you. therichpost.com

Comments

One response to “How to get related posts of any post type on single post page?”

  1. Alina Avatar
    Alina

    good post..