Ayd In Ayd In - 5 months ago 22
PHP Question

how can I display specific user's post in wordpress?

I want to display popular user's posts in wordpress and I can get the popular users ids with this code ;

$user_query = new WP_User_Query( array(
'meta_query' => array (
array (
'key' => 'wp_popular_users',
'value' => $user_id,
'compare' => 'LIKE'
)
) ) );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->ID. ', ';
}
}


and I can show the posts with this code ;

$the_query = new WP_Query( array(
"posts_per_page"=>8,
"post_type"=>"post",
'author__in'=> array(POPULAR USER IDS HERE!!!),
"paged"=>$paged
) );

while ( $the_query->have_posts() ){
$the_query->the_post();
get_template_part( 'template-parts/content', 'profile-post' );
}


but I couldn't combine those two code, I need to insert user IDS in 'author__in'=> array(POPULAR USER IDS HERE!!!), for example : 1,5,7

how can I make it ? thanks for answers

Answer

Can you try to see if this works?

    $popularUsers = Array();
    $user_query = new WP_User_Query( array( 
        'meta_query' => array (
            array (
                'key' => 'wp_popular_users',
                'value' => $user_id,
                'compare' => 'LIKE'
                )
            ) ) );
    if ( ! empty( $user_query->results ) ) {
        foreach ( $user_query->results as $user ) {
            echo  $user->ID. ', ';
            $popularUsers[] = $user->ID;
        }
    }

    $the_query = new WP_Query( array(
                    "posts_per_page"=>8,
                    "post_type"=>"post",
                    'author__in'=> $popularUsers,
                    "paged"=>$paged
                    ) );

                while ( $the_query->have_posts() ){
                    $the_query->the_post();
                    get_template_part( 'template-parts/content', 'profile-post' );
                }