• Resolved Brian DiChiara

    (@briandichiara)


    Maybe I’m over thinking this, but my brain is more geared to database table connections, so I’m having trouble figuring out how to setup my connections.

    My biggest struggle is I want 2 of the same type of connection. I want “people” CPT to be connected to “business” CPT (many-to-many), but I also want a *primary* “person”. Would this be accomplished by creating 2 connections, or 1 connection with a highly customized admin interface, and then store some kind of meta data along with the “person”?

    My goal is to make the data the easiest to retrieve as possible later on. What are your suggestions for this type of setup? I’m sure it’s been done before, I’m just struggling with setting this up the right way. Thanks.

    https://www.ads-software.com/extend/plugins/posts-to-posts/

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

    (@scribu)

    You could create a single connection type, with a ‘primary’ custom field defined. The problem would be that the UI (a checkbox) would allow you to have multiple “primary” connections per item.

    You could define 2 connection types and use the built-in ‘cardinality’ setting for one of them. In that case, you could have a person be the ‘primary’, while not necessarily having a regular ‘many-to-many’ connection, which might or might not be acceptable.

    Thread Starter Brian DiChiara

    (@briandichiara)

    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>';

    Thread Starter Brian DiChiara

    (@briandichiara)

    I realize now I was using get_related() when i needed to use get_connected()

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Complex Configuration, Need Advice’ is closed to new replies.