WordPress.org

Support

Support » Plugins and Hacks » Posts 2 Posts » [Plugin: Posts 2 Posts] Getting all metadata entries

[Plugin: Posts 2 Posts] Getting all metadata entries

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.

    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.
Skip to toolbar