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

    https://www.ads-software.com/extend/plugins/posts-to-posts/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author scribu

    (@scribu)

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

    Thread Starter landwire

    (@landwire)

    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

    (@scribu)

    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.