WordPress.org

Support

Support » Plugins and Hacks » Posts 2 Posts » [Resolved] User to user ?

[Resolved] User to user ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • From the documentation: “With this plugin, it’s also possible to create many-to-many relationships between posts and users.”

    Please explain what you understand by “user 2 user relation”.

    Ted

    Just looking for an example for that too. That is what I mean by user 2 user:

    Posts 2 Posts: Version 1.1

    I’m pleased to announce that the Posts 2 Posts plugin now also supports posts-to-users connections. (It supports users-to-users connections as well, but there’s no UI for them).

    I do not need a UI just an example of how to make a connection, use metadata etc.
    Thanks,
    Sascha

    Ok, sorry for asking before, but maybe it would be good to post this as an example in the Wiki?

    To set-up a connection type:

    p2p_register_connection_type( array(
            'name' => 'user_to_office',
            'from' => 'user',
            'to' => 'user',
    		'fields' => array( // this creates extra meta fields that can be added to a connection - use to distinguish between connections by meta
    					'role' => array(
    						'type' => 'text',
    					),
    		)
        ) );

    Then to make a connection:

    // Create connection
    $from = 1; // user ID
    $to = 2; // user ID
    p2p_type( 'user_to_office' )->connect( $from, $to, array(
        'role' => 'KING!'
    ) );

    To display information:

    $user = 1;
    		$user_query = new WP_User_Query( array(
    			'connected_type' => 'user_to_office',
    			'connected_items' => $user,
    			) );
    
    		// User Loop
    		if ( ! empty( $user_query->results ) ) {
    			foreach ( $user_query->results as $user ) {
    				echo '<p>' . $user->display_name . '</p>';
    				echo 'Role: ' . p2p_get_meta( $user->p2p_id, 'role', true );
    			}
    		} else {
    			echo 'No users found.';
    		}

    Hope that helps somebody. Remember no UI for user2user!

    Thanks landwire, and for adding it to the wiki too.

    Would you give me an example of a situation where one might use a user-to-user connection?

    Ted

    1. Friend Connections
    2. ‘Following’ connections
    3. role ‘office’ to role ’employee’
    4. role ‘office’ to role ‘boss’

    Plenty really. Anything where you have user accounts and then have accounts for certain roles, then connect them together.
    Sascha

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] User to user ?’ is closed to new replies.
Skip to toolbar