How to add custom meta title and meta description in Wordpress?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

By therichpost

Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.