I need to get the author information, who having maximum number of post(custom post type).

Here is the code I tried to get the result.

 $author_query = new WP_User_Query(array ( 
                           'orderby' => 'post_count', 
                           'order' => 'DESC', 
                ));

$authors = $author_query->get_results();

foreach ( $authors as $author ) {
    echo $author->ID;
    echo $author->display_name;
}

The result is getting as post-count of normal posts. Here I have custom post type instead of default post. I need to get the result based on my custom post type’s post count..

3 Answers
3

You can try with the following query, where the author data as well as the post count can be retrieved.

$sql = "SELECT SQL_CALC_FOUND_ROWS wp_users.ID,post_count FROM wp_users RIGHT JOIN (SELECT post_author, COUNT(*) as post_count FROM wp_posts WHERE ( ( post_type="custom-post-type" AND ( post_status="publish"  ) ) ) GROUP BY post_author) p ON (wp_users.ID = p.post_author) WHERE 1=1 ORDER BY post_count DESC";

$result = $wpdb->get_results($sql,OBJECT);
print_r($result); 

Leave a Reply

Your email address will not be published. Required fields are marked *