0

Using a third party plugin that allows users to follow each other, we can retrieve the users being followed like so (commented):-

<?php if (have_posts()) : ?>
        <?php global $userpro_social;

        $following = $userpro_social->following( get_current_user_id() ); //get users the current user is following
        print_r($following) ?> // print the array so we can see who we're following


        <?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?>     
        <?php query_posts( array( 'author'=> ??? , 'paged' => $paged, ) ); ?>      

        <?php while ( have_posts() ) : the_post() ?>


            <?php if ( has_post_format( 'video' )) {
                    get_template_part( 'video-post' );
                }elseif ( has_post_format( 'image' )) {
                    get_template_part( 'image-post' );
                } else {
                   get_template_part( 'standard-post' );
                }

            ?>

        <?php endwhile;?>

        <?php endif; ?>

So this would output something like this

Array ( [24] => 1 [1] => 1 )

ie, we're following users with the ID of 1 and 24, simple enough?

The part I'm lost on is this

<?php query_posts( array( 'author'=> ??? , 'paged' => $paged, ) ); ?>   

How do I actually output posts from those users, it's already stored in the array so I think it should be easy enough, but I just can't figure it out even after reading the codex.

1 Answer 1

0

First of all, I suggest don't use query_posts. (No one is really a WPSE user if had no read this answer).

So you can use WP_Query with author__in parameter in combination with array_keys PHP function.

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;    

$query = new WP_Query(
  array( 'author__in' => array_keys( $following ), 'paged' => $paged )
);

while ( $query->have_posts() ) : $query->the_post();
  // your loop here
endwhile;

wp_reset_postdata();

PS: you don't need to open and close php tags on every line.

1
  • Great answer, thanks. The reason I'm use query_posts and not WP_query is because I'm using infinite scroll plugin, that doesn't accept WP_query arrays, so the easiest thing to do is modify the main query with query_posts.
    – andy
    Commented Feb 27, 2014 at 9:40

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.