WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] 'auto-draft' posts break (one-to-one) connections (8 posts)

  1. TCBarrett
    Member
    Posted 1 year ago #

    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.

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    Confirmed.

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

  3. TCBarrett
    Member
    Posted 1 year ago #

    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.

  4. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  5. TCBarrett
    Member
    Posted 1 year ago #

    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.

  6. TCBarrett
    Member
    Posted 1 year ago #

    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 );
              endif;
            else :
              p2p_delete_connection( $cnx->p2p_id );
            endif;
          endforeach;
        endif;
      endforeach;
      echo "<p>Cleared</p>";
      die();
    }

    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 :)

  7. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    You could do it in a single SQL query:

    $wpdb->query( "
      DELETE FROM $wpdb->p2p
      WHERE p2p_type = 'YOUR_CONNECTION_TYPE'
      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.

  8. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.