Title: [Plugin: Posts 2 Posts] Custom admin boxes example mess up WP_Query
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Custom admin boxes example mess up WP_Query

 *  [faulty](https://wordpress.org/support/users/faulty/)
 * (@faulty)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/)
 * Hi, I’m not sure if this is my fault. When I tried the example code in [https://github.com/scribu/wp-posts-to-posts/wiki/Custom-admin-boxes](https://github.com/scribu/wp-posts-to-posts/wiki/Custom-admin-boxes)
   it work nicely, except the editor ended up displaying the content (text content,
   title and permalink) of the linked post instead of the one I’m editing, and if
   I click update, it will save the displayed data over the current post.
 * I’m using p2p to link subpost() to post. This is my setup
 *     ```
       p2p_register_connection_type(
       		array(
       			'name' => 'subpost_to_post',
       			'from' => 'subpost',
       			'to' => 'post',
       			'cardinality' => 'many-to-one',
       			'admin_box' => 'false',
       			'admin_column' => 'any',
       		)
       	);
       ```
   
 * Everything else as per the example.
 * Everything back to normal if I comment out `p2p_list_posts( p2p_type( 'posts_to_pages')-
   >get_connected( $post->ID ) );`
 * Do I need to call something to flush or reset `WP_Query` after using p2p?
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

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

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682202)
 * It would be useful to see the whole code. Please put it in a [http://pastebin.com/](http://pastebin.com/)
 *  Thread Starter [faulty](https://wordpress.org/support/users/faulty/)
 * (@faulty)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682219)
 * Thanks for the reply scribu
    As the plugin is big, I have made one with the necessary
   code to repeat the error. [http://pastebin.com/YfHgNiKB](http://pastebin.com/YfHgNiKB)
 * So repeat the error,
    1. Create a post, and a subpost 2. create link from subpost
   to post 3. click on the link generated by `p2p_list_posts` in “Link SubPost” 
   from posts or “Link Post” from subpost 4. Go back to both post and subpost, now
   the content are swapped. 5. Comment out `p2p_list_posts( p2p_type('subpost_to_post')-
   >get_connected( $post->ID ));`, then it’s back to normal
 *  Thread Starter [faulty](https://wordpress.org/support/users/faulty/)
 * (@faulty)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682395)
 * I’ve found the cause of the problem at line 333 of api.php, under `function p2p_list_posts()`.`
   $GLOBALS['post']` is replace with `$post` and left as is. I believe you’ll need
   to keep a copy of it, then restore it upon exiting the loop. Maybe that’s why
   wp picked up the title/content of the related post and displayed at the current
   post editor.
 * I’ve manage to fix the bug by doing this
 *     ```
       $_post = clone $GLOBALS['post'];
       		p2p_list_posts( p2p_type('subpost_to_post')->get_connected( $post->ID ) );
       		$GLOBALS['post'] = $_post;
       ```
   
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682396)
 * > I believe you’ll need to keep a copy of it, then restore it upon exiting the
   > loop.
 * This is done using the `wp_reset_postdata()` call at the end.
 *  Thread Starter [faulty](https://wordpress.org/support/users/faulty/)
 * (@faulty)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682397)
 * But in your `api.php`, you did call `wp_reset_postdata()` towards the end of `
   p2p_list_posts()`. What could have been the problem?
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682398)
 * Well, the problem is that `wp_reset_postdata()` actually uses `$wp_query->post`,
   which is not what you want in the metabox.
 * This should fix it:
 * [https://github.com/scribu/wp-posts-to-posts/commit/a2db540cdfbb642036b59a56799f26090c5d84bd](https://github.com/scribu/wp-posts-to-posts/commit/a2db540cdfbb642036b59a56799f26090c5d84bd)
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682400)
 * Actually, to avoid any kind of problems of this nature, `p2p_list_posts()` won’t
   touch any globals from now on:
 * [https://github.com/scribu/wp-posts-to-posts/commit/2b29d429fb2bedfb17ac144fd2232a08a81ea81f](https://github.com/scribu/wp-posts-to-posts/commit/2b29d429fb2bedfb17ac144fd2232a08a81ea81f)
 *  Thread Starter [faulty](https://wordpress.org/support/users/faulty/)
 * (@faulty)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682401)
 * Nice, thanks for the speedy update

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

The topic ‘[Plugin: Posts 2 Posts] Custom admin boxes example mess up WP_Query’ 
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/)

 * 8 replies
 * 2 participants
 * Last reply from: [faulty](https://wordpress.org/support/users/faulty/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-custom-admin-boxes-example-mess-up-wp_query/#post-2682401)
 * Status: not resolved