0

So I am using the WPMU Dev friends plugin which adds functionality for WP members to search for friends and to add new friends etc. There is a specific function defined in the PHP file which says it lets you check if users are friends. The code for this function is here:

/**
 * Utility function to determine is a user is friends with another user.
 *
 * @since 1.2.3
 *
 * @param
 * tmp_uid - int - Current user ID
 * tmp_friend_uid - int - Friend user ID
 * @return returns the value of 'friend_approved' field. 1 - Approved, 0 - Pending, null - no status
 */
function friends_check_status($tmp_uid, $tmp_friend_uid) {
    global $wpdb;

    $sql = $wpdb->prepare("SELECT friend_approved FROM " . $wpdb->base_prefix
        . "friends WHERE user_ID = %d AND friend_user_ID = %d", $tmp_uid, $tmp_friend_uid);
    //echo "sql=[". $sql ."]<br />";
    return $wpdb->get_var($sql);
}

I would like to run this function to check against author_id to check if the post author and user are friends if they are, I would the posts to be displayed but if not they should be hidden from the feed. Is this possible to do and does anyone have any ideas about how to go about it? Could I simply substitute tmp_friend_uid for the_author_meta( 'ID' ); or something?

1 Answer 1

0

Not sure if you're still looking for an answer to this as it's been 7+ months since it was asked, but I was looking to accomplish the same thing and using the function you highlighted above, I came up with the code below that, when placed inside of the loop, works perfectly.

First, declare your global $current_user if you haven't already:

<?php global $current_user; wp_get_current_user(); ?>

And then after you create your $args for pulling posts with whatever criteria are necessary, inside of the loop, wrap your output for posts in the simple if check:

<?php while (have_posts()) : the_post();
// declaring $post_author on each post to check if the post's author is friends with the current user, and if so, displays the post. 
$post_author = get_the_author_meta( 'ID' );
$current_user_friends = friends_check_status($current_user->ID, $post_author);

if($current_user_friends):
?>

// ... all of your HTML and PHP calls to post fields here

<?php endif; 
endwhile; ?>

This hides any posts where the current user is not friends with the post author. The only downside to this is that it's not checking BEFORE posts are pulled, so if you're expecting to have 10 posts displayed but the user isn't friends with the 10 posts pulled, they'll see 0. Definitely need to come up with a way to run the check BEFORE the array of posts is created for output to ensure that users are getting as many posts to be displayed as possible...

1
  • Looks pretty good to me so have marked it as correct. Unfortunately our deadlines were pressing so we moved to a different plugin for this functionality but thank you anyway!
    – dwinnbrown
    Commented Jul 31, 2016 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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