Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

UPDATE:

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:

<ul>
  <?php
  add_action('pre_user_query','user_query_count_post_type');
  $showrooms = get_users('orderby=post_count&role=company&order=desc');
  remove_action('pre_user_query','user_query_count_post_type');

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

  <?php endforeach; ?>
</ul>
share|improve this question

1 Answer 1

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:

add_action('pre_user_query','user_query_count_post_type');
$users = get_users('orderby=post_count');
remove_action('pre_user_query','user_query_count_post_type');
share|improve this answer
    
Tried that, but getting the error Fatal error: Cannot use object of type WP_User_Query as array –  Hassan Feb 13 '13 at 21:54
    
@Hassan, my bad , i think i fixed it. –  Bainternet Feb 13 '13 at 22:28
    
this still does not work. Do I need to modify anything in the code? –  Hassan Feb 14 '13 at 4:28
    
@Hassan yeah you need to modify cpt with the name of your custom post type . –  Bainternet Feb 14 '13 at 8:17
    
Yup, I obviously changed that since my first try, but still getting no results. What I'm doing is I've put the above code snippet in my functions.php file and then used get_users('orderby=post_count'); normally in a page template. What am I doing wrong? –  Hassan Feb 15 '13 at 4:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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