• 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 15 replies - 1 through 15 (of 16 total)
  • Plugin Author scribu

    (@scribu)

    Just replace get_posts() with new WP_Query and the second loop should work.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    It’s working a little better, but it’s displaying the same set of posts on every user’s buddypress profile. I think it’s showing all connections. I would like to display only the posts connected to User A on User A’s profile page.

    Plugin Author scribu

    (@scribu)

    It’s not clear from the code you posted where the $user variable is set. It should preferably be a WP_User instance.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    This code is all on the functions.php page and hooked in. This is the first time I’ve really worked with loops, hooks, and objects so I might need a little more specific guidance.

    How and where would I set the $user variable? Could you give me an example of using an instance of WP_User?

    Plugin Author scribu

    (@scribu)

    I’m not very familiar with BuddyPress, so I don’t know how you could get hold of the current user in a profile page; try asking in the BuddyPress support forums (down for maintenance, ATM).

    That said, I’m pretty sure you’re not supposed to put the loop code directly in functions.php.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    Ok cool. Thank you for helping me out, this gives me a new direction to try out. I’ll try placing the loop directly on the bp-member-header.php and look into the buddypress codex for some current user code if needed.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    I’m still trying to figure this out. To be honest I really don’t understand what you meant by

    It’s not clear from the code you posted where the $user variable is set. It should preferably be a WP_User instance.

    Is $user similar to $post or is it a unique variable that we need to set?

    Also, could you explain to me what kind of values the parameter “connected_items” is looking for?

    BTW, Buddypress does have a bp->displayed_user object which we might could user to identify which user.

    Plugin Author scribu

    (@scribu)

    Also, could you explain to me what kind of values the parameter “connected_items” is looking for?

    post objects or WP_User objects.

    BTW, Buddypress does have a $bp->displayed_user object which we might could user to identify which user.

    Then pass that, instead of $user.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    Here’s my current code on member-header.php

    <?php // show badges from posts-to-posts functions
    	global $bp;
    	// print_r($bp->displayed_user);
    	$connected = new WP_Query( array(
    	  'connected_type' => 'multiple_authors',
    	  'connected_items' => ($bp->displayed_user),
    	  'suppress_filters' => false,
    	  'nopaging' => true,
    	) );
    
    	// Display connected badges
    	if ( $connected->have_posts() ) :
    		?>
    		<ul class="badges">
    		<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
    			<li class="badge"><a href="<?php the_permalink(); ?>"><?php the_content(); ?></a></li>
    		<?php endwhile; ?>
    		</ul>
    		<?php
    		wp_reset_postdata();
    		endif;
    	 ?>
    	</ul>
        </p>
    	<?php
    }

    I can’t figure out why it’s not working. print_r($bp->displayed_user) does show the correct user so I know it’s working. When I comment out ‘connected_items’ => ($bp->displayed_user), the loop produces all connections so I know everything else is working. But once I uncomment it the loop doesn’t produce anything.

    Do you see what I’m missing?

    Plugin Author scribu

    (@scribu)

    Please post the result of the print_r() call.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    stdClass Object ( [id] => 1 [domain] => https://drawalot.com/learn/members/Shaun/ [userdata] => stdClass Object ( [ID] => 1 [user_login] => smartw [user_pass] => $P$BPsNdP6fItUhJk36j/MjrIXcIuQ1RN/ [user_nicename] => Shaun [user_email] => [email protected] [user_url] => https://plus.google.com/109053629809587529403 [user_registered] => 2010-12-04 16:45:04 [user_activation_key] => [user_status] => 0 [display_name] => Shaun McMillan ) [fullname] => Shaun McMillan )

    Plugin Author scribu

    (@scribu)

    Well, that’s a stdClass object, not a WP_User object.

    Try passing $bp->displayed_user->id to ‘connected_items’.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    I tried that already. Just tried it again and it still doesn’t work. It really should be working by now.

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    I must be missing something. Is there anyway I can see all the connections listed out in the database? Maybe I can try with an instance of WP_User just to test and see if that works. Would I use wp_get_current_user()?

    Thread Starter Shaun McMillan

    (@shaunmcmillan)

    I tried some other users and it’s actually working! Not sure why it doesn’t work with my own id, but as long as it works for everyone else I’m happy.

    Thank you for the help Scribu ??

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