-
Notifications
You must be signed in to change notification settings - Fork 261
Related posts
scribu edited this page Oct 20, 2011
·
15 revisions
Continuing from the Basic usage tutorial, let's say that when viewing a single post, you don't want to show the connected page, but other posts connected to the same page.
First, you will have to create a function in your functions.php
theme file:
<?php
function get_related_posts( $post_id ) {
global $my_connection_type;
// Get the page connected to the post
$related_pages = $my_connection_type->get_connected( $post_id );
// No related posts if there's no page connecting them
if ( !$related_pages->have_posts() )
return array();
// Get the page ids
$page_ids = wp_list_pluck( $related_pages->posts, 'ID' );
// Get the other posts connected to the same pages, except the current post
return $my_connection_type->get_connected( $page_ids, array(
'post__not_in' => array( $post_id ),
) );
}
It takes a post id and returns a WP_Query instance with the related posts.
Now that we have the logic for finding related posts, all we need to do is display them:
<?php p2p_list_posts( get_related_posts( get_queried_object_id() ) ); ?>
The above code would go in single.php