Posts 2 Posts
Getting all metadata entries (4 posts)

  1. landwire
    Posted 3 years ago #

    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....


  2. scribu
    Plugin Author

    Posted 3 years ago #

    1. Get all connections.
    2. Group connections by their 'role' in a nested array.
    3. Display each group of connections.

  3. landwire
    Posted 3 years ago #

    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] ) )
    	$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>
    	<?php }
    } ?>
  4. scribu
    Plugin Author

    Posted 3 years ago #

    Something like that. Just replace that last part:

    foreach ( $all_posts_by_role as $role ) {
    	if ( !isset( $all_posts_by_role[$role] ) )
    	$posts_by_role = $all_posts_by_role[$role];

    with this:

    foreach ( $all_posts_by_role as $role => $posts_by_role ) {

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic