In short, this version improves the connection type based API introduced in 0.9 and removes the old API.
Connection Type IDs
Previously, you had to keep track of connection type instances yourself, making it hard to work with more than one connection type.
Old:
global $my_connection_type;
$my_connection_type = p2p_register_connection_type( array(
'from' => 'post',
'to' => 'page'
) );
New:
p2p_register_connection_type( array(
'id' => 'posts_to_pages',
'from' => 'post',
'to' => 'page'
) );
And, to get a list of connected items, instead of:
global $my_connection_type;
$connected = $my_connection_type->get_connected( get_queried_object_id() );
you can now do it in one line:
$connected = p2p_type( 'posts_to_pages' )->get_connected( get_queried_object_id() );
One-To-Many Connections
There’s a new ‘cardinality’ argument:
p2p_register_connection_type( array(
'from' => 'post',
'to' => 'page',
'cardinality' => 'one-to-many',
'reciprocal' => true
) );
When editing a page, you will only be able to connect it to a single post and only to a post that doesn’t have a connected page yet.
'cardinality' => 'many-to-one'
has the same effect, in reverse.
Arbitrary query variables
You can now distinguish between connection types based on any query variable, not just ‘post_type’:
$types = array(
'bug' => 'Bug',
'feature' => 'Feature'
);
foreach ( $types as $type => $title ) {
p2p_register_connection_type( array(
'from' => 'contact',
'to' => 'ticket',
'to_query_vars' => array(
'meta_key' => 'type',
'meta_value' => $type
),
'title' => array( 'from' => $title ),
);
}
The above code will create two meta boxes: in one, contacts will only be able to create connections to ‘bug’-type tickets and in the other only to ‘feature’-type tickets.
There’s a matching ‘from_query_vars’ argument as well, so you could, for example, create a connection type between posts from category X to posts with tag Y etc.
Removed old API
I went ahead and removed the old API. Here’s the migration path:
Old | New |
---|---|
p2p_connect() | p2p_type( ‘my_connection_type’ )->connect() |
p2p_disconnect() | p2p_type( ‘my_connection_type’ )->disconnect() |
p2p_get_connected() | p2p_type( ‘my_connection_type’ )->get_connected() |
The benefit, of course, is that each of the new methods enforces the rules establised when registering the connection type.
Finally, I set up a code reference site, just to play around with DocBlox. Let me know if you find it useful.