Plugin Author
scribu
(@scribu)
Confirmed.
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
(@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 );
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 🙂
Plugin Author
scribu
(@scribu)
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.
Plugin Author
scribu
(@scribu)
This should be fixed in the development version (1.4-alpha).