scribu / wp-posts-to-posts

Efficient many-to-many connections between posts, pages, custom post types, users.
http://wordpress.org/plugins/posts-to-posts/
969 stars 260 forks source link

p2p_type( 'MY_CONNECTION' )->connect creates metas twice if defaut #538

Open tcarterfrance opened 7 years ago

tcarterfrance commented 7 years ago

Hi, great plugin, thanks,

When I do : $p2p_id = p2p_type( 'posts_to_infos' )->connect( $post_id, $info_to_add, array( 'featured' => $featured, 'reply' => $reply, 'minimum' => $minimum, 'maximum' => $maximum, ) );

I get the featured and reply metas twice in table PREFIX_p2pmeta, one with default value one with correct value.

Work-a-round :

$p2p_id = p2p_type( 'posts_to_infos' )->connect( $post_id, $info_to_add, array() );
p2p_update_meta( $p2p_id, 'featured', $featured ); p2p_update_meta( $p2p_id, 'reply', $reply ); p2p_update_meta( $p2p_id, 'minimum', $minimum ); p2p_update_meta( $p2p_id, 'maximum', $maximum );

My connection functions are :

function ci_connection_types() {

...

p2p_register_connection_type( array(
    'name' => 'posts_to_infos',
    'from' => 'post',
    'to' => 'infos',
    'admin_box' => array(
    'show' => 'any',
    'context' => 'advanced',
    ),

    'fields' => array(
        'featured' => array( 
            'title' => 'Phare',
            'type' => 'select',
            'values' => array( 'Phare','Pas phare' ),
            'default_cb' => 'ci_info_feature_status',

        ),
        'reply' => array(
            'title' => 'Reponse',
            'type' => 'text',
            'default_cb' => 'ci_info_excerpt',
        ),          
        'minimum' => array(
            'title' => 'Minimum',
            'type' => 'text',
        ),          
        'maximum' => array(
            'title' => 'Maximum',
            'type' => 'text',
        ),          
    ),

)

);

} add_action( 'p2p_init', 'ci_connection_types' );

function ci_info_feature_status( $connection, $direction ) { global $post;

$key = ( 'from' == $direction ) ? 'p2p_to' : 'p2p_from';

$post = get_post( $connection->$key );
setup_postdata( $post );

$featured_info = get_post_meta($post->ID, 'featured_info', true );   
$status = ($featured_info == 'featured_info') ? 'Phare' : 'Pas phare';

return $status;

}