Title: [Plugin: Posts 2 Posts] Reciprocal Problem
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Reciprocal Problem

 *  Resolved [Derek Perkins](https://wordpress.org/support/users/ploobers/)
 * (@ploobers)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-reciprocal-problem/)
 * I have nearly 10,000 rows in my _p2p table and have been using P2P almost since
   the very beginning. During that time, there have been API and storage changes,
   in addition to me making multiple site changes. Over that period, my calls to
   register relationships have changed. Sometimes one post type was ‘from’ and another
   time it was ‘to’. At this point, I have nearly 20 calls to p2p_register_connection_type
   in my code.
 * My problem is that with my current setup, only some of my connections are being
   returned correctly. For example, I have post types ‘club’ and ‘player’ (talking
   about soccer teams). I connected 43 players to a specific club, but when I make
   the call to WP_Query, it returns zero results. If I check the SQL code from that
   query and change the ‘p2p_from’ and ‘p2p_to’ fields, all 43 players are correctly
   returned, so I know that the information is still correct in my database.
 * My problem is that while I can change the ‘from’ and ‘to’ in my p2p_register_connection_type
   and it returns the 43 players correctly, my issue is that in some cases, the 
   original ‘from’ and ‘to’ return the correct results. Over time, I hadn’t paid
   attention to the ordering of my registration fields because I assumed that with
   the reciprocal field on, it wouldn’t matter one bit. Now it is coming back to
   bite me in the butt.
 * Am I going to need to write a script to go through and change each connection
   one by one to make sure that ‘player’ is always in the ‘from’ field and ‘club’
   is in the ‘to’ field? It seems like this issue should be able to be resolved 
   with some changes to the SQL calls, but that may result in less efficient queries.
 * Any thoughts?
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

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

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-reciprocal-problem/#post-2540444)
 * Yeah, the ‘reciprocal’ flag has a long history.
 * At the very beginning, ‘reciprocal’ meant that a single connection was stored
   both ways, which was very inefficient.
 * Also, it used to control where the admin box was displayed. Now, there’s a dedicated‘
   admin_box’ arg for that.
 * Now, it’s only useful when you try to connect a single post type to itself, for
   example `'from' => 'page', 'to' => 'page'`.
 * So, to answer your question, I think it would be best if you ran a script that
   flipped the connections as needed.
 *  Thread Starter [Derek Perkins](https://wordpress.org/support/users/ploobers/)
 * (@ploobers)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-reciprocal-problem/#post-2540468)
 * Ok great. I’ll post my switching code here after I’m done.
 *  Thread Starter [Derek Perkins](https://wordpress.org/support/users/ploobers/)
 * (@ploobers)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-reciprocal-problem/#post-2540520)
 * This isn’t the greatest code ever written, but if you are worried that you have
   post types on both sides of a connection and need to clean things up, this code
   should do it for you. All you need to do is change the names of the post types
   you want to switch the order of and it will do its magic.
 *     ```
       global $wpdb;
       $sql = "SELECT p.post_type AS PT_from, pt.post_type AS PT_to, p2p.p2p_type, p2p.p2p_id, p2p.p2p_from, p2p.p2p_to
       FROM sr_p2p p2p
       INNER JOIN sr_posts p ON p2p.p2p_from = p.ID
       INNER JOIN sr_posts pt ON p2p.p2p_to = pt.ID
       ";
       $entries = $wpdb->get_results( $sql );
       echo "<table>";
       foreach( $entries as $entry ) {
       	$p2p_type = $entry->PT_from.":".$entry->PT_to;
       	switch( $p2p_type ) {
       		case "posttype1:posttype2":
       		case "posttype3:posttype1": p2p_switch_from_to( $entry->p2p_id, $entry->p2p_to, $entry->p2p_from ); break;
       	}
   
       }
       echo "</table>";
   
       function p2p_switch_from_to( $p2p_id, $post_type_from, $post_type_to ) {
       	global $wpdb;
       	$wpdb->update('sr_p2p', array( 'p2p_from' => $post_type_from, 'p2p_to' => $post_type_to ), array( 'p2p_id' => $p2p_id ) );
       	echo "<tr><td>$p2p_id :: New From: $post_type_from | New To: $post_type_to</td><td>$entry->p2p_type</td></tr>";
       }
       ```
   

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

The topic ‘[Plugin: Posts 2 Posts] Reciprocal Problem’ 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/)

 * 3 replies
 * 2 participants
 * Last reply from: [Derek Perkins](https://wordpress.org/support/users/ploobers/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-reciprocal-problem/#post-2540520)
 * Status: resolved