List of users who have a made a post in a cpt

[ad_1]

Hi @candell

I would start by creating a function, that retrieves all the users, who has a published post within your custom post type. Now, I don’t know what your CPT is called, so I’ve filled it out with “YOUR_CPT” in the code below:

function get_users_with_published_posts() {
$args = array(
'post_type' => 'YOUR_CPT',
'post_status' => 'publish', // Only get the posts with "published" status
'numberposts' => -1,
);

$posts = get_posts($args);
$users_with_published_posts = array();

foreach ($posts as $post) {
$user_id = $post->post_author;
$user = get_userdata($user_id);

if (in_array('authority', $user->roles)) { // I assume "authority" is your userrole
$users_with_published_posts[$user_id] = $user->display_name;
}
}

return $users_with_published_posts;
}

Please make sure the function name is unique – I’ve just added something for the sake of providing some code. Now, in order to show the authors, you can use the function above like this:

$users = get_users_with_published_posts();
foreach ($users as $user_id => $display_name) {
echo $display_name . '<br>';
}

The code above only gives you an user-id and “display_name” – if you need any other info, you should put it in the “$users_with_published_posts”-array. I haven’t tested the code above – it’s just “on the fly” – so naming is also a bit fluffy. Please adjust for your needs.

Best regards
Aris

Thanks for the idea, all working now

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer