WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Loop for displaying connected users (17 posts)

  1. Shaun McMillan
    Member
    Posted 1 year ago #

    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.

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

  3. Shaun McMillan
    Member
    Posted 1 year ago #

    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.

  4. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

  5. Shaun McMillan
    Member
    Posted 1 year ago #

    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?

  6. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  7. Shaun McMillan
    Member
    Posted 1 year ago #

    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.

  8. Shaun McMillan
    Member
    Posted 1 year ago #

    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.

  9. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  10. Shaun McMillan
    Member
    Posted 1 year ago #

    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?

  11. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    Please post the result of the print_r() call.

  12. Shaun McMillan
    Member
    Posted 1 year ago #

    stdClass Object ( [id] => 1 [domain] => http://drawalot.com/learn/members/Shaun/ [userdata] => stdClass Object ( [ID] => 1 [user_login] => smartw [user_pass] => $P$BPsNdP6fItUhJk36j/MjrIXcIuQ1RN/ [user_nicename] => Shaun [user_email] => shaundmcmillan@gmail.com [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 )

  13. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

    Try passing $bp->displayed_user->id to 'connected_items'.

  14. Shaun McMillan
    Member
    Posted 1 year ago #

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

  15. Shaun McMillan
    Member
    Posted 1 year ago #

    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()?

  16. Shaun McMillan
    Member
    Posted 1 year ago #

    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 :D

  17. mkbir
    Member
    Posted 1 year ago #

    @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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic