Ok, here is my configuration and it seems to be working “ok” but i’m having a few issues:
p2p_register_connection_type( array(
'name' => 'company_primary_contact',
'from' => $this->contacts->post_type,
'to' => $this->companies->post_type,
'title' => array(
'to' => __('Primary Contact', IMB_I18N),
'from' => __('Primary Contact On', IMB_I18N)
),
'cardinality' => 'one-to-many',
'from_labels' => array(
'singular_name' => __( 'Primary Contact', IMB_I18N ),
'search_items' => __( 'Search Contacts', IMB_I18N ),
'not_found' => __( 'No primary contact.', IMB_I18N ),
'create' => __( 'Set Primary Contact', IMB_I18N )
),
'to_labels' => array(
'singular_name' => __( 'Connected Companies (Primary)', IMB_I18N ),
'search_items' => __( 'Search Companies', IMB_I18N ),
'not_found' => __( 'No companies.', IMB_I18N ),
'create' => __( 'Set Company (Primary)', IMB_I18N )
)
) );
p2p_register_connection_type( array(
'name' => 'company_contacts',
'from' => $this->contacts->post_type,
'to' => $this->companies->post_type,
'title' => array(
'to' => __('Additional Contacts', IMB_I18N)
),
'prevent_duplicates' => false,
'from_labels' => array(
'singular_name' => __( 'Contact', IMB_I18N ),
'search_items' => __( 'Search Contacts', IMB_I18N ),
'not_found' => __( 'No contacts.', IMB_I18N ),
'create' => __( 'Add Contact', IMB_I18N ),
)
) );
I’ve got this in the admin interface ok with some adjustments to labels and meta box priority, however my troubles are on the Table view. I’m actually having 2 troubles.
On my Companies page, I want a column called “contacts” and that would display the total # of connected contacts for that company. My first problem is my # is wrong. My second problem is I want a link to the Manage Contacts Admin page that only shows the connected items. Here is my code for the column:
$connection_primary = 'company_primary_contact';
$connection = 'company_contacts';
$primary = p2p_type( $connection_primary )->get_related( $post );
$related = p2p_type( $connection )->get_related( $post );
$total_contacts = count($primary->posts) + count($related->posts);
echo '<a href="'.admin_url('edit.php?post_type='.$imb_plugin->contacts->post_type).'&connected_items='.$post->ID.'&connected_type[]='.$connection_primary.'&connected_type[]='.$connection.'&connected_direction=from">'.$total_contacts.'</a>';
I wasn’t sure if calling “connected_type[]” would work, but I tried this on a different connection and I’m still getting invalid data and my links don’t work:
$connection = 'company_projects';
$related = p2p_type( $connection )->get_related( $post );
echo '<a href="'.admin_url('edit.php?post_type='.$imb_plugin->projects->post_type).'&connected_items='.$post->ID.'&connected_type='.$connection.'&connected_direction=from">'.count($related->posts).'</a>';