How To Create Related Post WordPress Plugin

Some weeks ago i need to create releated post for single.php . After some tries and looking idea in the internet . Here is the plugin idea : find related post based on categories where related post based on post date before the post single which become the center

To do that we need function : is_single , wp_get_post_categories , get_posts , and add_filter for the_content to put related post under content.

Some Notes : to get post ID on single just use global $post , get_posts is used get post based on criterias, wp_get_post_categories to get categories of existing post

Because we need to convert post date we need function strtotime and date in Y-m-d format . Ok here is the full code :

<?php
/*
Plugin Name: ZZ Related Post
Plugin URI: http://www.wpamanuke.com
Description: Sample Related Post based on category
Version: 1.0.1po
Author: WPAmaNuke
Author URI: https://wpamanuke.com
*/

// ADD BOTTOM RELATED ARTICLE ON THE POST SINGLE
function zz_related_post() {
   // check if single
   if (is_single()) {
		global $post;
		
		$post_ids = [];
		$prev_post = get_previous_post();
		$next_post = get_next_post();
		$i = 0;
		$post_ids[$i] = $post->ID;
		$i++;
		if ($prev_post) {
			$post_ids[$i] = $prev_post->ID;
			$i++;
		}
		/*
		if ($next_post) {
			$post_ids[$i] = $next_post->ID;
		}
		*/
		
		$a = strtotime($post->post_date . " 0 month");
		$time = date("Y-m-d",$a);
		$cat = wp_get_post_categories($post->ID);
		
		
		$related = get_posts( 
			array( 
				'category__in' => $cat, 
				'numberposts' => 3, 
				'post__not_in' => $post_ids,
				//'meta_key'=>'_thumbnail_id',
				'orderby' => 'DATE',
				'order' => 'DESC',
				'date_query' => array(
					'before' => $time 
				)
			) 
		);
		
		
		if ($related) {
			echo '<div class="zz-related-post">';
			echo '<h3>Related Post</h3>';
			
			foreach( $related as $post ) {
				setup_postdata($post);
				?>
				<div class="zz-related-post_block">
				<article>
					<?php if ( has_post_thumbnail() ) { ?>
						<figure class="zz-related-post_image"> 
							<a class="zz-image-link" href="<?php the_permalink(); ?>" title="">
								<?php the_post_thumbnail( 'medium' ); ?>
							</a>
						</figure>
					<?php } ?>
					<div class="zz-related-post_title">
						<h4 class="zz-meta-title"><a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a></h4>
					</div>
				</article>    
				</div>
				<?php
			}
			echo '</div>';
			
		}
		wp_reset_postdata();
   }
}
// End Function Related POST

function zz_after_content($content) {
	ob_start();
	zz_related_post();
	$after_content = ob_get_clean();
	$custom_content = $content . $after_content;
	return $custom_content;
}
add_filter( 'the_content', 'zz_after_content');


?>

Just see your single post . And Related post will be on the bottom after content.

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.