Posts 2 Users

scribu edited this page Mar 5, 2013 · 8 revisions

With this plugin, it's also possible to create many-to-many relationships between posts and users.

Here's how you would define such a connection type:

p2p_register_connection_type( array(
    'name' => 'multiple_authors',
    'from' => 'article',
    'to' => 'user',
    'to_query_vars' => array( 'role' => 'editor' )
) );

You can leave out the 'to_query_vars' => array( 'role' => 'editor' ) line, in which case all users can be connected.

To get a list of users connected to a certain post, you can write:

$users = get_users( array(
  'connected_type' => 'multiple_authors',
  'connected_items' => $post
) );

To get a list of posts connected to a certain user, you can write:

$posts = get_posts( array(
  'connected_type' => 'multiple_authors',
  'connected_items' => $user,
  'suppress_filters' => false,
  'nopaging' => true
) );

Note: Currently (P2P 1.5), the connections metabox will not show up when editing a user. See https://github.com/scribu/wp-posts-to-posts/issues/261