Support » Plugin: Posts 2 Posts » [Plugin: Posts 2 Posts] 'auto-draft' posts break (one-to-one) connections

  • Resolved TCBarrett


    1. Set up a one-to-one connection between users and posts (post type).
    2. Click ‘Add New’ in (custom) post admin
    3. Select user in connections box
    4. Refresh page
    5. Try to select same user again

    The relationship is stored in p2p table, and thus user cannot be reselected. The auto-draft hangs around and eventually gets deleted. I manually deleted the auto-draft, but the user was not released.

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



    When you say you “manually deleted the auto-draft”, do you mean you did a direct SQL query?

    Yes, deleted the auto draft post using sql. Not any associated data (like postmeta). Was just a cheap test to see how you handled orphaned relationships.

    Plugin Author scribu


    Yeah, so there seem to be two problems here:

    1. Auto-drafts shouldn’t count when enforcing cardinality.
    2. Orphanned connections should be ignored.

    Thanks for reporting.

    It’s a great plugin!

    Was hoping to incude it in a project, but the auto-draft issue will cause too many support issues right now.

    Just in case anyone wants a quick work-around, I added an item to the menu in my project to call this function:

    add_action( 'wp_ajax_awards_clear_unwanted_p2p_connections', 'tcb_fix_p2p_post_to_user_orphans_and_autodrafts' );
    function tcb_fix_p2p_post_to_user_orphans_and_autodrafts(){
      foreach ( P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype ) :
        if( $ctype->object['from'] == 'post' && $ctype->object['to'] == 'user' ) :
          $connections = p2p_get_connections( $p2p_type );
          foreach( $connections as $cnx ) :
            if( $post = get_post($cnx->p2p_from) ) :
              if( $post->post_status == 'auto-draft' ) :
                p2p_delete_connection( $cnx->p2p_id );
            else :
              p2p_delete_connection( $cnx->p2p_id );
      echo "<p>Cleared</p>";

    It clears orphaned and auto-draft posts in post-to-user connections. Possibly not great for large sets, but I’m hoping it will surfice as a temp fix until the plugin gets updated 🙂

    Plugin Author scribu


    You could do it in a single SQL query:

    $wpdb->query( "
      DELETE FROM $wpdb->p2p
      AND p2p_from IN (
        SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft'
    " );

    Just be careful not to mix user ids with post ids.

    Plugin Author scribu


    This should be fixed in the development version (1.4-alpha).

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Plugin: Posts 2 Posts] 'auto-draft' posts break (one-to-one) connections’ is closed to new replies.