Only show posts created by authors who are friends of the current user

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 .”]
“;

$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!

  • Predrag Dubajic
    • Support

    Hey Brendan,

    Hope you’re doing well today :slight_smile:

    I will need to call in some of our second level support dev guys to have a look at this and help you out.

    Please note that our developers are quite busy lately and their response times are longer for custom requests, if you’re in a hurry to get this sorted out then you can post a job in our job board where you can hire a developer to assist further:

    https://premium.wpmudev.org/wpmu-jobs/

    Please note that, no WPMU official staff members are allowed to work in the job board.

    Best regards,

    Predrag

  • Jude
    • DEV MAN

    Howdy @brendan

    Hope you’re keeping well and sorry about the delay here

    Why not simply use your $friendsID array in a WP Query like this

    $the_query = new WP_Query( array( 'author' => implode( ",", $friendsIdArray )  ) );

    and use the $the_query variable in a posts loop like below.

    // The Query
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    } else {
    // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    Hope that helps

    Cheers

    Jude

  • Brendan
    • WPMU DEV Initiate

    Hey Jude,

    Thanks for the help its very much appreciated. I am just wondering if you could explain where abouts we put that code in the post so it works and we dont mess up any of the other site functions. Also want to confirm that the code we put in the question (the function called friends_get_list) should be placed in the functions.php file and then called into the index.php file?

    This is the loop called in the index.php file

    http://pastebin.com/b17wFnMT

  • Brendan
    • WPMU DEV Initiate

    Hey jude,

    All we have done so far is installed the plugins we haven’t loaded it into the index.php nor content.php. We were only provided with the code in our question by someone else on the forum and haven’t gone about using it because we don’t know how to basically…. could you add it in at the right points to the index.php?

  • Adam Czajczyk
    • Support Gorilla

    Hello Brendan!

    It seems that Jude is currently temporary unavailable but I took a look at your pastebin an the code here and (assuming that the function in question is working fine) the new loop code could be like this:

    <div class="row" id="poll-content-wrapper">
    <main id="content" class="col-md-8 col-lg-9 content-area" role="main">

    <?php
    //The Query
    $the_query = new WP_Query( array( 'author' => implode( ",", $friendsIdArray ) ) );

    // The Loop
    if ( $the_query->have_posts() ) {
    ?>
    <?php while ( $author_posts->have_posts() ) : $author_posts->the_post() ?>
    <div class="col-xs-12 col-sm-6 col-lg-4 masonry-item">
    <?php get_template_part( 'content', get_post_format() ); ?>
    </div>
    <?php endwhile ?>
    <!-- #masonry --></div>
    <?php nuthemes_content_nav( 'nav-below' ); ?>

    <?php else : ?>

    <?php get_template_part( 'no-results', 'index' ); ?>

    <?php endif; ?>
    wp_reset_postdata();

    <!-- #content --></main>

    <?php //get_sidebar(); ?>
    <!-- .row --></div>

    That’s a code taken from your pastebin and enhanced with Jude’s solution. I wouldn’t consider this a “ready to go” solution as I’m pretty sure there’s more template files in your theme than just an index.php and the loop in this form probably shouldn’t be used for each and every page of your site but I hope this will give you a starting point.

    Best regards,

    Adam

  • Brendan
    • WPMU DEV Initiate

    Thanks for that Adam but when we try to load the site using that we get

    Parse error: syntax error, unexpected ‘else’ (T_ELSE) in /home/u248157444/public_html/outrankd/wp-content/themes/pieces/index.php on line 72

    A copy of the pastebin below

    http://pastebin.com/4Bvbs5sy

    This is how we loaded the function in the friends.php file:

    /*
    function friends_get_list($tmp_uid, $friend_status=1) {
    global $wpmudev_friends;

    return $wpmudev_friends->friends_get_list($tmp_uid, $friend_status);

    }
    */

    //Function for 'show friends posts only query'
    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;
    }

  • Brendan
    • WPMU DEV Initiate

    Hey Michael,

    Thanks for that code but now we are getting this error

    Warning: implode(): Invalid arguments passed in /home/u248157444/public_html/outrankd/wp-content/themes/pieces/index.php on line 19

    Showing up on the websites main page

  • Brendan
    • WPMU DEV Initiate

    Thank you everyone for your help,

    I do believe it is working and will let you know if their are any problems with it. The only other thing i cant work out is how to get our search bar to show users and their profile pic’s any advice on that would be greatly appreciated.

    Thanks

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.