How to get authors who have added the post in the specific custom post type

I have a custom post called event and I have to show all the authors who have added the post. I have tried the below code but I am getting all the users on the page.

For example. There are 4 users called A, B, C, and D on the website and A and B added the post.

Now I have to get only A and B users but below code getting all the users like A,B,C,D

Any idea how to get the user to add the post in the event post type.

$users = get_users( array( 'fields' = array( 'ID','display_name' ),'order' = 'ASC', 'who' ='authors' ));

    foreach ($users as $user){
    $userid=$user-ID;
    //echo  $post_count = count_user_posts($userid);
    echo 'lia href='.esc_url(get_author_posts_url($userid)).''.$user-display_name.'/a/li';
         }

Topic list-authors theme-development plugins Wordpress

Category Web


When you say "who have added the post", are you meaning all users who have written at least 1 post of the event post type? If so, you can get all the authors of all events like so:

$event_posts = get_posts([
    'post_type' => 'event', // Or whatever your event post type is
    'numberposts' => -1,
]);

// Get all the author (user) IDs of all events
$event_author_ids = wp_list_pluck( $event_posts, 'post_author' );

// Now get all the user objects
$users = get_users([
    'include' => $event_author_ids,
]);

Alternatively, if you are meaning the author of the current event post, there can only ever be one author per-post, and you can just use the author template tags whilst in the loop.

echo get_the_author_link();
// Get author <a /> HTML

echo get_the_author_meta( $prop );
// Get author property e.g. display_name, user_email, first_name etc.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.