Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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 );
share|improve this answer
    
Yes, currently, the only effect that 'reciprocal' => true has is displaying the metabox on the other end of the connection. –  scribu Jun 18 '11 at 15:41

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.

share|improve this answer
    
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 '11 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 '11 at 15:41
    
Got it, thanks guys! –  Mike Jun 18 '11 at 23:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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