Creating relationships between multiple content types [closed]

Why not do…

$my_post_types = array(
    'post', 
    'page', 
    'topic', 
    'case', 
    'note', 
    'question', 
    'therapy_guideline'
);

p2p_register_connection_type( array(
    'name' => 'my_post_relationships',
    'from' => $my_post_types,
    'to' => $my_post_types,
    'sortable' => 'any',
    'reciprocal' => false,
    'cardinality' => 'many-to-many',
    'title' => array(
        'from' => 'Children',
        'to' => 'Parent',
        ),
    'admin_column' => 'any',
    )
);

Or something similar?

Edit:

A way to possibly get your multiple boxes without all that typing, using the aforementioned array.

foreach($my_post_types as $post_type){
    // use line below if you don't ever need to relate posts to posts, pages to pages, etc.
    $temp_array = array_diff($my_post_types, array($post_type));

    p2p_register_connection_type( array(
        'name' => 'my_'.$post_type.'_connections',
        'from' => $temp_array, // use $my_post_types if you didn't define $temp_array
        'to' => $post_type,
        'sortable' => 'any',
        'reciprocal' => false,
        'cardinality' => 'many-to-many',
        'admin_column' => 'any',
        )
    )
}

The temp array makes it so each post type is related to every other post type, but not itself. Not sure if that’s something you want or not, easy enough to remove.

Leave a Comment