15

I want to build a TV series database using WordPress. I have followed some tutorials and I have two custom post types: one for movies, one for and series. I followed this post for the structure.

My question is: how can I make the relationship between the movies and series post types?

  • First of all, you are NOT creating a whole database, you are just creating a WordPress' Custom Post Type. And within WordPress scope you have many opportunities categorizing them, sorting them etc. And all of them are done with a good internal relationship. So, the relationship what you are talking about is already there, you just have to implement it in the way you need. So, I would suggest you to implement the post_type as directed, and then edit the question with your real query. – Mayeenul Islam Jan 5 '14 at 12:53
  • @MayeenulIslam, "implement the post_type as directed" what do you mean? – CoalaArmy Jan 5 '14 at 12:59
  • As directed in your provided link-thread, or by using a simple generator like: this Post Type Generator. – Mayeenul Islam Jan 5 '14 at 13:03
  • @MayeenulIslam : I am not convinced that "the relationship what you are talking about is already there". By default, there is no direct relationship between different post types. – s_ha_dum Jan 5 '14 at 15:07
  • @s_ha_dum Sorry, I missed some part of his question where he already said that he had two post_types. It's my mistake. I's trying to make him clarify with his detail. I missed that the detail is already there. :( I's talking about the inner relationship of taxonomy and custom post type if defined. – Mayeenul Islam Jan 5 '14 at 15:52
20

Using a Plugin

Some very good plugins for relationships:

Using a Metabox

You can build a simple relationship using metaboxes:

add_action( 'admin_init', 'add_meta_boxes' );
function add_meta_boxes() {
    add_meta_box( 'some_metabox', 'Movies Relationship', 'movies_field', 'series' );
}

function movies_field() {
    global $post;
    $selected_movies = get_post_meta( $post->ID, '_movies', true );
    $all_movies = get_posts( array(
        'post_type' => 'movies',
        'numberposts' => -1,
        'orderby' => 'post_title',
        'order' => 'ASC'
    ) );
    ?>
    <input type="hidden" name="movies_nonce" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>" />
    <table class="form-table">
    <tr valign="top"><th scope="row">
    <label for="movies">Movies</label></th>
    <td><select multiple name="movies">
    <?php foreach ( $all_movies as $movie ) : ?>
        <option value="<?php echo $movie->ID; ?>"<?php echo (in_array( $movie->ID, $selected_movies )) ? ' selected="selected"' : ''; ?>><?php echo $movie->post_title; ?></option>
    <?php endforeach; ?>
    </select></td></tr>
    </table>
}

add_action( 'save_post', 'save_movie_field' );
function save_movie_field( $post_id ) {

    // only run this for series
    if ( 'series' != get_post_type( $post_id ) )
        return $post_id;        

    // verify nonce
    if ( empty( $_POST['movies_nonce'] ) || !wp_verify_nonce( $_POST['movies_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    // check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    // check permissions
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;

    // save
    update_post_meta( $post_id, '_movies', array_map( 'intval', $_POST['movies'] ) );

}

And then, to get the movies relationship as a list for series posts:

$series = new WP_Query( array(
    'post_type' => 'movies',
    'post__in' => get_post_meta( $series_id, '_movies', true ),
    'nopaging' => true
) );

if ( $series-> have_posts() ) { while ( $series->have_posts() ) {
    $series->the_post();
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ></a></li>
    <?php
} }
  • 3
    I like the idea with the metaboxes (trying to avoid plugins)! – CoalaArmy Jan 5 '14 at 13:16
  • Without creating another post, can we add to this how to create a permalink from this parent/child relationship? For example: http://domain.com/series-name/movie-name? – Xtremefaith Apr 8 '15 at 5:23
  • @Xtremefaith this can be done with other code. There's a plugin I created for categories. Maybe you can use as a reference. wordpress.org/plugins/hierarchical-urls – vmassuchetto Aug 7 '16 at 11:38
5

I recommend the Posts 2 Posts plugin, which I've just started using.

It allows you to create many-to-many relationships between posts and page types, meaning you can link movies to series, and any other CPTs you may create.

This plugin also allows you to create connection metadata which will allow you to get finer detail when creating your connections. It is quite flexible in its usage, allowing for control over admin metaboxes, connection types, and ways to display your connections on the front end. Lastly, it is well-documented.

  • I'll take a look at it. – CoalaArmy Jan 5 '14 at 12:58
  • Iwas looking for sth. similar and tried various solutions. I have to say that this one (Post 2 Post) was perfect, it might not be what a 'non-programmer' would be looking for, but if you are looking more for sth. like a library to use in your template, than this is just perfect, well documented, well integrated, get it! ;) – Larzan Dec 18 '14 at 17:59
  • I did a small plugin for post relations but wordpress.org/plugins/softmixt-relations – Softmixt Jul 4 '17 at 7:37
2

Unfortunately, the Posts 2 Posts plugin is deprecated and no longer maintained. There is a new alternative plugin for that MB Relationships. It's inspired by P2P and provides a similar API to create relationships between posts, terms and users.

MB Relationships supports bi-directional relationships by default and use a custom table to store relationships (like P2P) for a better performance (than post meta).

It's worth taking a look at the plugin.

protected by Community Jan 31 '17 at 23:31

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.