0

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 '<li><a href="'.esc_url(get_author_posts_url($userid)).'">'.$user->display_name.'</a></li>';
         }

1 Answer 1

0

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.
2
  • Yes, I have tried the first solution and it's working.. just want to know why you have used the include in the get_users Jun 2, 2022 at 11:02
  • Check the WP_User_Query docs. The include param is for getting users (authors) by their ID (which is the post authors of any event post). Jun 3, 2022 at 23:01

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.