Skip to content

Reciprocal connections

scribu edited this page Jul 11, 2011 · 21 revisions

The 'reciprocal' flag

When registering a connection type, you can set the 'reciprocal' flag:

	p2p_register_connection_type( array( 
		'from' => 'post',
		'to' => 'page',
		'reciprocal' => true
	) );

Now you will see the connection box whenever you edit a post or a page and you can create a connection from either place and it will show up in both.

Important: Connections are created in a single direction, from postpage.

The 'connected' query var

Say you want to connect posts to other posts:

	p2p_register_connection_type( array( 
		'from' => 'post',
		'to' => 'post',
		'reciprocal' => true
	) );

And then you want to display all the connections related to a certain post. Instead of using 'connected_to' or 'connected_from', you can use the 'connected' query var:

$connected = new WP_Query( array(
	'post_type' => 'post',
	'connected' => get_queried_object_id()
) );

This will return current_postother_post connections, as well as other_postcurrent_post.

Clone this wiki locally