WordPress makes it easy to find posts in the same category, with the same tag, most recent posts and so on. You may want to refine what other posts you recommend when viewing a post. For example, in one category you have 20 posts but 5 of them are a series on a specific topic for which you also have other posts but not in this particular series (so using tags does not help too much). When viewing any of these 5 posts, you want to recommend the other 4 in the same series.
A relationship between these posts can be made using a custom field (each post in the series will have the same custom field). After setting up this information you can use a simple code to find the related posts.
In the example below, the custom field name is post-series. The script will display all posts with the same custom field, excluding the current post (you don’t want to have the same post the visitor is seeing in the recommended list)
// Get post-series value
$post_series = get_post_meta($post->ID, 'post-series', true);
// Save the current post ID (to exclude it from the list)
$current_post_id = $post->ID;
// Setup query
$args = array(
'meta_key' => 'post-series',
'meta_value' => $post_series
);
// Get all posts with the same custom field
$post_series_query = new WP_Query( $args );
if ( $post_series_query->have_posts() ) {
while ( $post_series_query->have_posts() ) {
$post_series_query->the_post();
if ( $current_post_id != $post->ID ) { // Exclude the current post
echo the_title();
}
}
wp_reset_postdata();
}