The default WordPress function get_users('orderby=post_count'); only orders users with the count of the posts they've made.

Any idea how to modify this to support custom post types as well?


I only want to query posts of type "company-item". Currently, this snippet is in my functions.php:

 function user_query_count_post_type($args) {
   $args->query_from = str_replace("post_type = post AND", "post_type IN ('company-item') AND ", $args->query_from);

And this is in my page template:

  $showrooms = get_users('orderby=post_count&role=company&order=desc');

  foreach ($showrooms as $showroom) : ?>
        <a href="<?php echo get_author_posts_url( $showroom->id ); ?>" ><img src="<?php echo $showroom->company_logo; ?>" title="<?php echo $showroom->company_name; ?>" /></a>

  <?php endforeach; ?>

2 Answers 2


you can try to replace the where clause of the query by hooking to pre_user_query. Something like:

function user_query_count_post_type($args){
    $args->query_from = str_replace("post_type = post AND", "post_type IN ('post','cpt') AND ", $args->query_from);

Usage ex:

$users = get_users('orderby=post_count');
  • Tried that, but getting the error Fatal error: Cannot use object of type WP_User_Query as array
    – Hassan
    Feb 13, 2013 at 21:54
  • @Hassan, my bad , i think i fixed it.
    – Bainternet
    Feb 13, 2013 at 22:28
  • this still does not work. Do I need to modify anything in the code?
    – Hassan
    Feb 14, 2013 at 4:28
  • @Hassan yeah you need to modify cpt with the name of your custom post type .
    – Bainternet
    Feb 14, 2013 at 8:17
  • 1
    str_replace("post_type = post AND" ... , should be str_replace("post_type = 'post' AND " ... then it will work, note the ' around post and the space after AND
    – Tim
    Jan 6, 2015 at 1:43

A bit late but, just in case someone needs it on the future. You could add a get_posts inside the normal foreach for users, to check if a given user has stuff published on posts, OR on the CPT you want:

if ( ! function_exists( 'contributors_author_list' ) ) :

function contributors_author_list() {

    $contributor_ids = get_users( array(
        'fields'  => 'ID',
        'orderby' => 'post_count',
        'order'   => 'DESC',
        'who'     => 'authors',
        'number' => '100'
    ) );

    foreach ( $contributor_ids as $contributor_id ) :

        $argos = array( 'author' => $contributor_id, 'post_type'=> array('post','CHANGE-HERE-YOUR-CPT') ); //change here the CPT
        $userposts = get_posts($argos);
        if ($count) {

<!-- HTML stuff -->    


This way you should be able to display authors who published either posts or Custom Post Types (or both).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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