0

I queried a list of users using WP_User_Query from which I got the avatar, name and a few ACF that I created for the user registration.

Now, I need to obtain a post list of each of the users in my results but I don't know how to do it.

This is my query WITHIN WP_User_Query:

$postArgs = array (
                'author__in'=> array(2,4,6), //Authors's id's you like to include
                'post_type' => 'post',
                'posts_per_page' => 5,
            );

            $postsquery = new WP_Query ( $postArgs); 

            while ( $postsquery->have_posts() ) : $postsquery->the_post();
echo '
        <li class="viral-note">
            <div class="row no-gutter">
                <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3"> <img src="' . get_the_post_thumbnail_url() . '" alt="" class="square-mini img-responsive"> </div>
                <div class="col-xs-8 col-sm-8 col-md-8 col-lg-8">
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12"> <span class="catego-tag">' . $category[0]->cat_name . '</span> </div>
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12"> <span class="excerpt">' . get_the_title() . '</span> </div>
                </div>
            </div>
        </li>';
            endwhile;wp_reset_query();

How can I store the users from my first query into an array and then pass it onto my second query?

1 Answer 1

1

If you pass in the fields parameter to your user query you can easily get an array of ids to the pass in to your wp query :

//WP User Query
$author_query = new WP_User_Query( array (
    'number' => 3,
    'fields' => 'ID'
));

$author_results = $author_query->results;   

//WP Query
$postArgs = array (
    'author__in'=> $author_results,
    'post_type' => 'post',
    'posts_per_page' => 5,
);

Your Answer

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

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