Title: [Plugin: Posts 2 Posts] Getting all metadata entries
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Getting all metadata entries

 *  [landwire](https://wordpress.org/support/users/landwire/)
 * (@landwire)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-getting-all-metadata-entries/)
 * Hi there,
 * I have a post type “building”, that gets connected to let’s say 20 different 
   users. As the users get connected to a certain “building” ID= 225 (via a front
   end form), they can choose their role by typing in a text, which is stored in
   the connection metadata field “role”. So I have 20 connections as below for that“
   building” with ID=225 with the following different entries for the metadata field“
   role”:
 * architect (1 users), Architekt (1 user), designer (3 users), engineer (2 users),
   fan (8 users), manager (1 user), owner (1 user), visitor (3users)
 * I know I can show a connection if I know the meta entries, but what happens if
   I do not know the meta entries?
 * How would I go about getting all the connection meta entries for “role” that 
   has been used for that particular connection type from that “building” with ID
   =225? Then I would like to show all users under the respective connection meta
   entry in “role”, so I can structure it as above.
 * Not sure if that is clear….
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-getting-all-metadata-entries/#post-2955528)
 * 1. Get all connections.
    2. Group connections by their ‘role’ in a nested array.
   3. Display each group of connections.
 *  Thread Starter [landwire](https://wordpress.org/support/users/landwire/)
 * (@landwire)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-getting-all-metadata-entries/#post-2955569)
 * Thanks for your quick reply! Have not had time to try it out yet, but would I
   use the old p2p_split_posts function to achieve 2. ?
    Will try this tonight….
 * 1. Get all connections from current post viewed:
 *     ```
       $connected = new WP_Query( array(
         'connected_type' => 'building_to_user',
         'connected_items' => get_queried_object(),
         'nopaging' => true,
       ) );
       ```
   
 * So all my connections from the “building” I am just viewing are in $connected.
 * 2. Group connections by field ‘role’ – Would I use the old p2p_split_posts function?
 *     ```
       /**
        * Split some posts based on a certain connection field.
        *
        * @param object|array A WP_Query instance, or a list of post objects
        * @param string $key p2pmeta key
        */
       function p2p_split_posts( $posts, $key ) {
       	if ( is_object( $posts ) )
       		$posts = $posts->posts;
   
       	$buckets = array();
   
       	foreach ( $posts as $post ) {
       		$value = p2p_get_meta( $post->p2p_id, $key, true );
       		$buckets[ $value ][] = $post;
       	}
   
       	return $buckets;
       }
   
       $all_posts_by_role = p2p_split_posts( $connected, 'role' );
       ```
   
 * 3. Display each group:
 *     ```
       foreach ( $all_posts_by_role as $role ) {
       	if ( !isset( $all_posts_by_role[$role] ) )
       		continue;
       	$posts_by_role = $all_posts_by_role[$role];
               echo $role;
               echo '<br>';
               foreach ( $posts_by_role as $post ) { ?>
                      <div class="post-block">
       				<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
       		</div>
       	<?php }
       } ?>
       ```
   
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-getting-all-metadata-entries/#post-2955584)
 * Something like that. Just replace that last part:
 *     ```
       foreach ( $all_posts_by_role as $role ) {
       	if ( !isset( $all_posts_by_role[$role] ) )
       		continue;
       	$posts_by_role = $all_posts_by_role[$role];
       ```
   
 * with this:
 *     ```
       foreach ( $all_posts_by_role as $role => $posts_by_role ) {
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘[Plugin: Posts 2 Posts] Getting all metadata entries’ is closed to new
replies.

 * ![](https://s.w.org/plugins/geopattern-icon/posts-to-posts_7a8e9d.svg)
 * [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/posts-to-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/posts-to-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/posts-to-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/posts-to-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/posts-to-posts/reviews/)

## Tags

 * [entries](https://wordpress.org/support/topic-tag/entries/)
 * [metadata](https://wordpress.org/support/topic-tag/metadata/)

 * 3 replies
 * 2 participants
 * Last reply from: [scribu](https://wordpress.org/support/users/scribu/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-getting-all-metadata-entries/#post-2955584)
 * Status: not resolved