• 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 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?

    https://www.ads-software.com/extend/plugins/posts-to-posts/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author scribu

    (@scribu)

    It would be useful to see the whole code. Please put it in a https://pastebin.com/

    Thread Starter faulty

    (@faulty)

    Thanks for the reply scribu
    As the plugin is big, I have made one with the necessary code to repeat the error. https://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

    (@faulty)

    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

    (@scribu)

    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

    (@faulty)

    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

    (@scribu)

    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

    Plugin Author scribu

    (@scribu)

    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

    Thread Starter faulty

    (@faulty)

    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.