Title: [Plugin: Posts 2 Posts] Find related posts sharing connection
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Find related posts sharing connection

 *  Resolved [oudein](https://wordpress.org/support/users/oudein/)
 * (@oudein)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/)
 * I am having great success with this excellent plugin (which I hope becomes part
   of the core!), thank you Scribu! However, I just can’t wrap my head around this
   one thing. I have a connection between two custom post types, from ‘PROJECTS’
   to ‘PEOPLE’. What I am trying to do is show, while viewing say Project A, other
   projects (Project B, Project C, etc.) related via the people connected to both
   projects.
 * I think I want to sort on the `'to' => array('people'),` meta_key, but am not
   sure what the meta_value would be… Otherwise, could I call a second query inside
   a `foreach` showing all the people related to the project? Really not sure.
 * I hope I am explaining myself clearly enough and will clarify if that would help.
 * I also have a question about merging the the results of multiple p2p queries,
   but will start that in another thread!
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2357923)
 * See [https://github.com/scribu/wp-posts-to-posts/wiki/Related-posts](https://github.com/scribu/wp-posts-to-posts/wiki/Related-posts)
 *  Thread Starter [oudein](https://wordpress.org/support/users/oudein/)
 * (@oudein)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2358034)
 * scribu, thanks for the quick response! Sorry for my delayed one. I reread the
   wiki article on Related Posts — the wiki has been invaluable in using p2p — and
   tried it again, but it still only returns the project that I am viewing (although
   there are shared people with other projects).
 * The connection type is:
 *     ```
       global $projects_to_people_connection;
       	$projects_to_people_connection = p2p_register_connection_type( array(
       		'from' => 'projects',
       		'to' => array('people'),
       		'context' => 'side',
       		'sortable' => '_order',
       		'prevent_duplicates' => true,
       		'title' => 'Project Members',
       		'fields' => array(
       			'project_lead' => array(
       				'title' => 'Project Lead',
       				'values' => array(
       					'1' => 'Lead',
       					'0' => ''
       				)
       			),
       			'project_contact' => array(
       				'title' => 'Project Contact',
       				'values' => array(
       					'1' => 'Contact',
       					'0' => ''
       				)
       			)
       		)
       	) );
       ```
   
 * The related posts function is:
 *     ```
       function get_project_associated_people( $post_id ) {
       	global $projects_to_people_connection;
       	$related_pages = $projects_to_people_connection->get_connected( $post_id );
   
       	if ( !$related_pages->have_posts() )
       		return array();
   
       	$page_id = $related_pages->posts[0]->ID;
   
       	return $projects_to_people_connection->get_connected( $page_id, array(
       		'post__not_in' => array( $post_id ),
       		) );
       	}
       ```
   
 * And the call is:
 *     ```
       <?php global $get_project_associated_people; ?>
       <aside>
       	<h2>Related Projects via get_project_associated_people</h2>
       	<?php p2p_list_posts(get_project_associated_people( get_queried_object_id() )) ?>
       		<figure>
       			<?php if ( has_post_thumbnail()) :?>
       				<a href="<?php get_permalink(); ?>" title="<?php the_title_attribute('echo=0');?>" >
       					<?php clean_wp_width_height(get_the_post_thumbnail(get_the_ID(),'large'));
       					<figcaption><?php the_title_attribute(); ?></figcaption>
       				</a>
       		</figure>
       <?php wp_reset_postdata();?>
       </aside>
       ```
   
 * Again, I have no problem returning the people associated with the project, or
   getting the metadata assigning the lead or contact to people, but the call above
   just returns the project itself.
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2358035)
 * Hm… it shouldn’t even return the current project, because of the `'post__not_in'
   => array( $post_id )` arg.
 *  Thread Starter [oudein](https://wordpress.org/support/users/oudein/)
 * (@oudein)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2358038)
 * I know! That’s particularly confusing.
 * (By the by, I cleaned up the commentary in the call, so there are some missing
   closures and such, but it’s basically the same that I am using.)
 * I have multiple versions of these three running for other connections and they
   are all fine, but this one is stumping me. I was thinking that perhaps I should
   try to modify the related posts function, but I am not sure how to instruct it
   to return the projects associated with the people, instead of simply the people
   themselves.
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2358090)
 * With the [development version](http://downloads.wordpress.org/plugin/posts-to-posts.zip)(
   0.9.6-alpha), you can just do this:
 *     ```
       function get_project_associated_people( $post_id ) {
       	global $projects_to_people_connection;
       	return $projects_to_people_connection->get_related( $post_id );
       }
       ```
   
 * And if you use [connection type ids](http://scribu.net/wordpress/posts-to-posts/p2p-0-9-5.html),
   even shorter:
 *     ```
       function get_project_associated_people( $post_id ) {
       	return p2p_type( 'projects_to_people' )->get_related( $post_id );
       }
       ```
   
 *  Thread Starter [oudein](https://wordpress.org/support/users/oudein/)
 * (@oudein)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2358091)
 * scribu, I was just writing when the page refreshed and I saw your response!
 * I was going to say that I saw the addition of ids to connections types, that 
   I am really impressed with your dedication and alacrity in problem solving with
   this plugin, and that (even though I was already planning to donate when I get
   paid from this current project) my donation amount has definitely increased! 
   Thank you so much for all your work on the plugin and the speed with which you
   help users. I know this sounds a bit ridiculous, but your behavior really could
   be a model for other WordPress plugin devs.
 * So. Ids are aweseome and will make creating flexible queries using connections
   so much easier. I will give the examples you wrote above a shot and let you know
   how it goes.
 *  Thread Starter [oudein](https://wordpress.org/support/users/oudein/)
 * (@oudein)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2358114)
 * Thaks, scribu, it worked perfectly. Now I just have to figure out how to merge
   separate query results.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘[Plugin: Posts 2 Posts] Find related posts sharing connection’ is closed
to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/posts-to-posts_7a8e9d.svg)
 * [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/posts-to-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/posts-to-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/posts-to-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/posts-to-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/posts-to-posts/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [oudein](https://wordpress.org/support/users/oudein/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-find-related-posts-sharing-connection/#post-2358114)
 * Status: resolved