So I have a function in Wordpress with a plugin that returns a list of friends as an array. The function code is here:

function friends_get_list($tmp_uid, $friend_status=1) {
    global $wpdb;

    $friend_status = intval($friend_status);
    if (($friend_status != 1) && ($friend_status != 0))
        $friend_status = 1;

    if ( $friends_list = get_transient( 'wpmudev-friends-'. $tmp_uid .'-'. $friend_status ) ) {
        return $friends_list;

    $query = $wpdb->prepare("SELECT friend_user_ID FROM " . $wpdb->base_prefix . "friends WHERE user_ID = %d AND friend_approved = %d",
        $tmp_uid, $friend_status);
    //echo "query=[". $query ."]<br />";
    $friends_list = $wpdb->get_col($query);
    set_transient( 'wpmudev-friends-'. $tmp_uid .'-'. $friend_status, $friends_list, 60 );

    return $friends_list;

I would then like to call this function to obtain the list of the current users friends and then check this against the authors of the displayed posts. I can do this like so: $friendsIdArray = friends_get_list( get_current_user_id(), 1 ) ; which should return all of the confirmed friends for the current user as long as I reference the function. I then want to include this in the Wordpress query to only show posts created by authors who are friends of the current user. I believe I could use this code to load the results into a variable: $query = new WP_Query( array( 'author' => implode( ",", $friendsIdArray ) ); but am unsure exactly where this should go in the wordpress loop to achieve the desired result. Any help is much appreciated!


You actually want author__in, and you can use the array. You shouldn't implode it:

$query = new WP_Query( 
    'author__in' => $friendsIdArray 

As far as where in the Loop this goes, I'm not sure I understand. This occurs before creating your a Loop, not in it.

  • Ah ok so could i just stick that in the index.php file? – dwinnbrown Dec 29 '15 at 16:46
  • Honestly, you have to tell me what the context is. I can't guess what you want to do or where you want to do it. – s_ha_dum Dec 29 '15 at 16:48
  • In the index.php file, the posts are displayed like this: <div class="col-xs-12 col-sm-6 col-lg-4 masonry-item"> <?php get_template_part( 'content', get_post_format() ); ?> </div> and I want to add it in there somewhere or is better to do this in the query.php file? – dwinnbrown Dec 29 '15 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.