• Resolved Shaun McMillan

    (@shaunmcmillan)


    I’m trying to find a loop that can display all of the posts connected to the user on the user’s buddypress profile page.

    Here’s my code

    // Assign badge to user meta using Post-to-Posts Plugin
    function my_connection_types() {
    	p2p_register_connection_type( array(
        'name' => 'multiple_authors',
        'from' => 'badge',
        'to' => 'user',
        //'to_query_vars' => array( 'role' => 'editor, author' )
    ) );
    }
    add_action( 'p2p_init', 'my_connection_types' );
    
    <?php // Looping through badges on user profile //////////////////// ?>
        <p class="badges"><strong>Badges:</strong>
    	<ul class="badges">
        <?php // show badges from posts-to-posts functions
    	$connected = get_posts( array(
    	  'connected_type' => 'multiple_authors',
    	  'connected_items' => $user,
    	  'suppress_filters' => false,
    	  'nopaging' => true,
    	) );

    Here’s the first display loop I tried

    add_action( 'bp_before_member_header_meta', 'display_custom_usermeta' ); // place the author box on member profile in buddypress
    
    function display_custom_usermeta() { ?>
    	<div class="custom_usermeta">
    <?php
    // Display connected badges
    	foreach ( $connected as $post ): ?>
    		<li class="badge"><a href="<?php echo($connected->post_url); ?>"><?php echo($connected->post_title); ?></a></li>
    	<?php endforeach; ?>
    	</ul>
        </p>
    }

    Here’s the second loop I tried

    if ( $connected->have_posts() ) :
    		?>
    		<h3>Badges:</h3>
    		<ul>
    		<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endwhile; ?>
    		</ul>
    		<?php
    		wp_reset_postdata();
    		endif; ?>
    	</ul>
        </p>
    	<?php
    }?>

    Unfortunately neither of these produces the intended results.

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

Viewing 16 replies (of 16 total)
  • @scribu I am having problem for past couple of days now, I can run the p2p plugin perfectly in my localhost but when I upload or activate the plugin in the server the same code that i write in the functions.php doesn’t runs at all. I meant to say is when i ‘Create connections programmatically’ like

    p2p_type( 'MY_CONNECTION_TYPE' )->connect( $from_id, $to_id, array(
    	'date' => current_time('mysql')
    ) );

    I am unable to do it in server but I can in localhost anyways.
    Thanks in advance

Viewing 16 replies (of 16 total)
  • The topic ‘Loop for displaying connected users’ is closed to new replies.