2

I'm using Posts 2 Posts plugin. I've got 2 custom types : movies and actors. I created a movie => actor connection so that for each movie I can see which actors play in.

But as far as I understand, in order to find out all the movies a particular actor has played in, you must create an actor => movie connection AS WELL.

So if create a The Dark Knight => Christian Bale connection, I MUST create a Christian Bale => The Dark Knight as well. Because otherwise I won't be able to know that Christian Bale played in that movie based on a "Christian Bale" search.

Is that correct ? If so, is there any way to make it less burdensome ?

2 Answers 2

4

To see connections on both edit screens, set reciprocal to true, but note this is for the UI only, it doesn't affect connections otherwise.

function my_connection_types() {
    if ( !function_exists( 'p2p_register_connection_type' ) )
        return;

    p2p_register_connection_type( array( 
        'from' => 'movies',
        'to' => 'actors',
        'reciprocal' => true
    ) );
}
add_action( 'init', 'my_connection_types', 100 );
1
  • Yes, currently, the only effect that 'reciprocal' => true has is displaying the metabox on the other end of the connection.
    – scribu
    Jun 18, 2011 at 15:41
5

No, you shouldn't need one each way.

You can query on either direction of the one connection, so something like this would get you all actors in Dark Knight:

$actors = new WP_Query( array(
  'post_type' => 'actors',
  'connected_from' => $dark_knights_post_id,
));

And this would get you all movies that Christian Bale has been in:

$bale_movies = new WP_Query( array(
  'post_type' => 'movies',
  'connected_to' => $christian_bales_post_id,
));

You can read more on the plugin's documentation page.

2
  • It seems it doesn't matter if I use connected_from or connected_to, they both work, or is this the result of setting reciprocal to true? I distinctly recall Scribu telling me reciprocal affected UI only.
    – Milo
    Jun 18, 2011 at 14:57
  • Yes, you only need to create the connection one way and then query it from either end. 'connected' => $some_post_id checks both ways, but is slightly slower.
    – scribu
    Jun 18, 2011 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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