How to Add Related Post in GeneratePress Theme

In this quick video I will show you how to add Related Posts to blog posts in GeneratePress theme.

Required Plugins

  • Make sure to install Code Snippets plugin to add the below custom PHP code.

Filter Posts by Category

function wpframer_related_posts_by_category() {
	if(is_single()) {
		// Get current post's categories or tags
		$current_post_id = get_the_ID();
		$categories = get_the_category($current_post_id);
		$tags = get_the_tags($current_post_id);

		// Set up the query arguments for related posts by categories
		$args = array(
			'post_type'      => 'post',
			'posts_per_page' => 7, // Number of related posts to show
			'post__not_in'   => array($current_post_id), // Exclude current post
			'orderby'        => 'rand', // Randomize related posts
		);

		// If categories are assigned to the current post, filter by category
		if ($categories) {
			$category_ids = array();
			foreach ($categories as $category) {
				$category_ids[] = $category->term_id;
			}
			$args['category__in'] = $category_ids;
		}

		// If tags are assigned to the current post, filter by tag
		/*if ($tags) {
			$tag_ids = array();
			foreach ($tags as $tag) {
				$tag_ids[] = $tag->term_id;
			}
			$args['tag__in'] = $tag_ids;
		}*/

		// Run the query
		$related_posts_query = new WP_Query($args);

		if ($related_posts_query->have_posts()) :

			echo '<div class="wpframer-related-posts" style="margin-top: 25px; margin-bottom: 25px;">';
        	echo '<h3>Read More...</h3>';
			echo '<ul>';
			while ($related_posts_query->have_posts()) : $related_posts_query->the_post();
				echo '<li>';
				echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
				echo '</li>';
			endwhile;
			echo '</ul>';

		endif;
	}
}

add_action('generate_after_content', 'wpframer_related_posts_by_category', 10);

Filter Posts by Tags

function wpframer_related_posts_by_tags() {
	if(is_single()) {
		// Get current post's categories or tags
		$current_post_id = get_the_ID();
		$categories = get_the_category($current_post_id);
		$tags = get_the_tags($current_post_id);

		// Set up the query arguments for related posts by categories
		$args = array(
			'post_type'      => 'post',
			'posts_per_page' => 7, // Number of related posts to show
			'post__not_in'   => array($current_post_id), // Exclude current post
			'orderby'        => 'rand', // Randomize related posts
		);

		// If tags are assigned to the current post, filter by tag
		if ($tags) {
			$tag_ids = array();
			foreach ($tags as $tag) {
				$tag_ids[] = $tag->term_id;
			}
			$args['tag__in'] = $tag_ids;
		}

		// Run the query
		$related_posts_query = new WP_Query($args);

		if ($related_posts_query->have_posts()) :

			echo '<div class="wpframer-related-posts" style="margin-top: 25px; margin-bottom: 25px;">';
        	echo '<h3>Read More...</h3>';
			echo '<ul>';
			while ($related_posts_query->have_posts()) : $related_posts_query->the_post();
				echo '<li>';
				echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
				echo '</li>';
			endwhile;
			echo '</ul>';

		endif;
	}
}

add_action('generate_after_content', 'wpframer_related_posts_by_tags', 10);

Leave a Comment