andy
andy

Reputation: 2754

Wordpress - getting posts from multiple authors?

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.

Upvotes: 2

Views: 3449

Answers (2)

Andy
Andy

Reputation: 995

Extending on Rafh

                $author_ids = array();

                // "Followings" is my custom code I'm using.
                $followings = $wpdb->get_results("SELECT * FROM x_follow WHERE user_id = ".$user_ID);
                foreach($followings as $following){

                    array_push($author_ids, $following->follow_user_id);

                }

                $args = array(
                    'posts_per_page'   => '30',
                    'offset'           => '0',
                    'post_type'        => 'post',
                    'post_status'      => 'publish',
                    'orderby'          => 'ID',
                    'order'            => 'DESC',
                    'author__in'       => $author_ids,
                );

                $posts = get_posts( $args ); 

                foreach($posts as $post){
                    var_dump($post);
                    // ENJOY :)
                }

Upvotes: 2

RafH
RafH

Reputation: 4544

If you want to retrieve posts from multiple authors, you need to use the author__in parameter

query_posts( array( 'author__in'=> array_keys($following) , 'paged' => $paged, ) );

Upvotes: 7

Related Questions