2

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..

1
  • Hey @prinz, welcome to Stackexchange! I would recommend not using the WP_User_Query to accomplish this task, rather try using the built-in wp-post functions (which allow you to specify post types and authors). I don't have to time to research it further, but look here: codex.wordpress.org/Template_Tags/get_posts . Research get_posts() or query_posts() functions.
    – Brent
    Jan 23 '18 at 17:06
2

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); 
1

Because the post_count attribute in the users table includes the total number of posts for the user and is not divided by post type in any way, I think that the only way would be for you to issue a custom SQL query.

<?php
//Get 10 most popular authors for post type
$topAuthors = $wpdb->get_results(
"
SELECT post_author
FROM $wpdb->posts WHERE post_type = 'topic'
GROUP BY post_author
ORDER BY COUNT(*) DESC
LIMIT 0,10
"
, OBJECT_K);

You can modify the order and limit attributes to the SQL query to hone in on exactly what you need.

Once you have the author ID's based on your criteria you can then query further using the include argument.

<?php
$authors = new WP_User_Query(array(
    'include' => array_keys($topAuthors)
));
foreach( $authors->get_results() as $author ) {
    echo $author->ID;
    echo $author->user_login;
}
0

There is actually a documented query parameter called has_published_posts which accepts an array of post types for WP_User_Query.

has_published_posts (boolean / array) - Pass an array of post types to filter results to users who have published posts in those post types. true is an alias for all public post types. Default is null. Since Version 4.3.

You should, therefore, just be able to run

$author_query = new WP_User_Query(array( 
    'orderby' => 'post_count', 
    'order' => 'DESC', 
    'has_published_posts' => array('custom_post_type')
));
1
  • That doesn't give you the post count for only this post type though.
    – swissspidy
    Jan 23 '18 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.