Creating relationships between multiple content types [closed]

I’m working on a site where there are multiple content types in addition to the built-in ones (see list below). I’d like to establish the ability to mark related content in any direction, from any content type to any other.

The content types are:

  • post
  • page
  • topic (from bbpress)
  • case (custom types from here on down)
  • note
  • question
  • therapy_guideline

Below I have code from my functions.php used to set up possible connections from posts to the other content types:

p2p_register_connection_type(
    array(
        'name' => 'post-to-post',
        'from' => 'post',
        'to' => 'post'
    )
);

p2p_register_connection_type(
    array(
        'name' => 'post-to-page',
        'from' => 'post',
        'to' => 'page'
    )
);

p2p_register_connection_type(
    array(
        'name' => 'post-to-topic',
        'from' => 'post',
        'to' => 'topic'
    )
);

p2p_register_connection_type(
    array(
        'name' => 'post-to-case',
        'from' => 'post',
        'to' => 'case'
    )
);

p2p_register_connection_type(
    array(
        'name' => 'post-to-note',
        'from' => 'post',
        'to' => 'note'
    )
);

p2p_register_connection_type(
    array(
        'name' => 'post-to-question',
        'from' => 'post',
        'to' => 'question'
    )
);

p2p_register_connection_type(
    array(
        'name' => 'post-to-therapy_guideline',
        'from' => 'post',
        'to' => 'therapy_guideline'
    )
);

That’s just for establishing relationships from posts to other content types. I’ve repeated these incantations to establish the other various connections – 28 in total. I cringe at the repetition. This could get really awkward if more content types are added, and if they need to be able to connect to all (or most) of the other types.

It’s occurred to me that I could use a loop to iterate over each of these content types and generate a connection to each other type, but that doesn’t feel right. I’ve also read about the reciprocal value, but that seems to be from a content type back to the same type. Is there a way to simplify creating connections between distinct content types without having to do it for each direction (from and to)?

My intent is to facilitate the selection of related content relative to the current content, via the backend when creating/editing content. Then to display a “Related Content” section following the display of said current content. These related content pieces could be across the spectrum of content types in use, but would be manually selected to ensure maximum relevance.

I am using custom taxonomies across these content types, and had considered using that for finding related content, but the taxonomies would still be too broad. Manual selection seems to be the way to go.

6 Answers
6

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