Reputation: 86
$args = array(
'orderby' => 'display_name'
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
echo '<ul>';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<li>'.$author->ID.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
I'm using post filter by post author. Above code displays all the users so need to display only authors who posted in the blog.
just like wp_list_authors() function but i need to get the author id intead of author name. because I need to create a dropdown list. While someone change the option, then I need to get the post by that author in AJAX
Upvotes: 0
Views: 1862
Reputation: 2471
Hope this help:
$posted = get_posts([
'post_type' => 'your_custom_post_type',
]);
$author_ids = [];
foreach ($posted as $post) {
$author_ids[] = $post->post_author;
}
$author_ids = array_unique($author_ids);
if (!empty($author_ids) )
{
echo '<ul>';
foreach ($author_ids as $user_id)
{
$author_info = get_userdata($user_id);
echo '<li>'.$user_id.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
Make sure to change post_type
to your post type and each user has both first_name
and last_name
.
There's a missing argument on Codex reference: has_published_posts
.
$args = [
'orderby' => 'display_name',
'has_published_posts' => true
];
$authors = new WP_User_Query($args);
...
It's better to look for inside class file because info on Codex is not always up-to-date.
Upvotes: 1