Ask your WordPress questions! Pay money and get answers fast! (more info)

Organizing/Indexing posts using buddypress follow? WordPress

  • SOLVED

Hello all!
I'm using the plugin[[LINK href="http://wordpress.org/plugins/buddypress-followers/"]]buddypress follow[[/LINK]] to allow my buddypress site users to follow each other. I also have it so that all of the users of my sites can make posts on the site into different post types like videos and images. What I would like to do is organize post by all the users the logged in user is following.

For example:

I have 2 custom post types: movies, books. Where all of the users of my site can submit posts about their favorite movie or book.

I have 5 members on the site. Lacey, Charlie, Zack, Zoe and Rachel.

I am following Lacey, Charlie and Zack and would like to organize the recent posts on post type movies by them (who I'm following) and exclude the rest of the sites members (Zoe and Rachel). While I'm logged in of course.

*not I do not want to use the buddypress activity stream.

I found out that you can organize the activity stream by following and followers in this [[LINK href="https://github.com/r-a-y/buddypress-followers/wiki/Create-your-own-follow-activity-loop"]]Github Wiki[[/LINK]] and tried to add that code to a wp_query thinking that the activity stream would be a custom post type(not sure if that's true):

<?php
$args = array( 'posts_per_page' => 6, 'post_type' => 'movies', 'user_id' => bp_get_following_ids( ));
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>


After trying that I'm really unsure as too what I have to do, or need to do. I'm a newer wordpress developer and on really understand theme development.

I also found a neat idea [[LINK href="http://wordpress.stackexchange.com/questions/41934/plugin-that-would-allow-wordpress-authors-to-follow-other-authors-and-query-a"]]Here[[/LINK]]

Thank you for the help! I hope this is a sufficient amount of money for this question. Please tell me me if it isn't. I was unsure of the amount of work needed to complete this question.

*EDIT* Here is the function. I found it in a folder called template tags:

/**
* Output a comma-separated list of user_ids for a given user's following.
*
* @param mixed $args Arguments can be passed as an associative array or as a URL argument string
* @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
* @uses bp_get_following_ids() Returns comma-seperated string of user IDs on success. Integer zero on failure.
*/
function bp_following_ids( $args = '' ) {
echo bp_get_following_ids( $args );
}
/**
* Returns a comma separated list of user_ids for a given user's following.
*
* This can then be passed directly into the members loop querystring.
* On failure, returns an integer of zero. Needed when used in a members loop to prevent SQL errors.
*
* Arguments include:
* 'user_id' - The user ID you want to check for a following
*
* @param mixed $args Arguments can be passed as an associative array or as a URL argument string
* @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
* @return Mixed Comma-seperated string of user IDs on success. Integer zero on failure.
*/
function bp_get_following_ids( $args = '' ) {

$defaults = array(
'user_id' => bp_displayed_user_id()
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

$ids = implode( ',', (array)bp_follow_get_following( array( 'user_id' => $user_id ) ) );

$ids = empty( $ids ) ? 0 : $ids;

return apply_filters( 'bp_get_following_ids', $ids, $user_id );
}

Answers (2)

2014-03-23

Naveen Chand answers:

Hi Corbin,

Please try this code in a new page template:


<?php // create an activity loop displaying only activity items that logged-in user ID is following
if ( bp_has_activities( array(
// change the user ID below to whatever
'user_id' => bp_get_following_id( get_current_user_id() )
) ) ) :?>
<?php while ( bp_activities() ) : bp_the_activity();?>
<?php locate_template( array( 'activity/entry.php' ), true, false );?>

    <?php endwhile; ?>
<?php endif; ?>



Notes: As per the plugin's wiki, we just need to pass the desired set of User IDs to the user_id variable before sending it to the loop. bp_get_following_id is the function we need to use. To this function, we need to specify to just get the IDs that are being followed by the logged in user. This is achieved by using get_current_user_id(). The rest is actually calling the default or standard loop of bp. I think you would not need New wp_query to do this if you are using the above code.

Hope this helps.

Naveen


Chris comments:

Thank you for the help Naveen.

I would use that, but I don't want to use the activities loop. I'm trying to use a loop for custom post types. I tried the code with the wp_query and it didn't work. Do you know of anyway of making a loop that this will work with my custom post types and not the activity loop?

<?php
$args = array( 'posts_per_page' => 6, 'user_id' => bp_get_following_id( get_current_user_id() ));
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>


I also tried 'user_id' => bp_get_following_id( bp_loggedin_user_id() )
Thanks again for your help


Naveen Chand comments:

Instead of 'user_id' try to use 'author__in' as mentioned below:


$args = array( 'posts_per_page' => 6, 'author__in' => bp_get_following_id( get_current_user_id() ));

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

the_title();

echo '<div class="entry-content">';

the_content();

echo '</div>';

endwhile;


Note that there is double underscores between author and in (Like this: author__in. Not like this: author_in). Also I hope you are declaring $bp as global variable before beginning the code as written below:


    global $bp;


2014-03-23

Hariprasad Vijayan answers:

Hello,

Try following code

<?php
$args = array('posts_per_page' => 6, 'post_type' => 'movies', 'author' => bp_get_following_ids('user_id='.bp_loggedin_user_id() ));
$loop = new WP_Query($args);
while ($loop -> have_posts()) : $loop -> the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>

Not tested here. Please check it and let me know.
Thanks.


Chris comments:

Thank you very much Hariprasad. This worked!


Hariprasad Vijayan comments:

Glad to hear.

Cheers,