Friends plugin find friends you have added

We brought a premium account to download your Friends plugin and was wondering how we go about checking if users are friends or just other users. Thank you

  • Milan

    Hello @Brendan,

    Welcome to WPMU DEV.

    I hope you are having a good day and thanks for the asking.

    Our Friends plugins provides different utility functions to satisfy different purposes.

    friends_check_status( $tmp_uid, $tmp_friend_uid )
    This function will take 2 arguments and return value of 'friend_approved' field. 1 - Approved, 0 - Pending, null - no status
    #1) tmp_uid - int - Current user ID
    #2) tmp_friend_uid - int - Friend user ID

    You can know more about this function in /friends.php file. There are some other functions available too,

    friends_get_list
    Utility function to get a list of friend user IDs

    friends_get_request_count
    Shows the number of friend requests on our menu item.

    For further information please review friends.php file of plugin.

    I hope this helps you.

    Please let me know if there is anything else I can help you with.

    Cheers,
    Milan Savaliya.

  • Milan

    Hello @Brendan,

    I am glad that it helped you.

    Dear member there is one utility function provided by Friends plugin API to get all the approved friends IDs as array.

    friends_get_list( $tmp_user_id, $request_status )
    Where,
    $tmp_user_id is id of use whose friends id needs to be fetched
    and,
    $request_status is friend request's status. Possible values for $request_status are,
    #1) '1' for returning approved only.
    #2) '0' for pending.

    Sample usage:

    $friendsIdArray = friends_get_list( get_current_user_id(), 1 ) ;

    Once you have friends IDs as array you can inject those value to WP_Query's author paramer like this,

    $friendsIdArray = friends_get_list( get_current_user_id(), 1 ) ;
    $query = new WP_Query( array( 'author' => implode( ",",  $friendsIdArray  ) );

    Once above code gets executed you'll have current' user's all friend's posts in $query variable. Then you can use standard wordpress loop to iterate those posts. This codex article will give you further idea about how to work with standard wordpress loop.
    https://codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop

    Let me know what you think about this one.

    Enjoy WPMU DEV.

    Cheers,
    Milan Savaliya.

  • Brendan

    Thank you very much for that,

    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!

  • Brendan

    Sorry, this is the code i was ment to put first in the above post.

    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 ."]
    ";
    $friends_list = $wpdb->get_col($query);
    set_transient( 'wpmudev-friends-'. $tmp_uid .'-'. $friend_status, $friends_list, 60 );

    return $friends_list;
    }

  • Milan

    Hello @Brendan,

    I hope you are having a good day and thanks for the asking.

    You can create simple shortcode for this functionality and then easily attach it to page content to display all the post of current user's friends.

    But don't worry I've created shortcode as plugin which you can activate to get desired results. Just extract attached file to /wp-content/plugins/ directory and then activate plugin via Plugins > Installed > WPMU DEV Friends Post

    Once you activate plugin just edit any page and use shortcode this way,

    [wpmudev_friends_posts max_posts="10"  ]

    Note: max_posts parameter will determine how many posts should be fetched.

    I hope this helps you.

    Please let me know if there is anything else I can help you with.

    Cheers,
    Milan Savaliya.