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>
you can try to replace the where clause of the query by hooking to
pre_user_query
. Something like:Usage ex:
A bit late but, just in case someone needs it on the future.
You could add a
get_posts
inside the normalforeach
for users, to check if a given user has stuff published on posts, OR on the CPT you want:This way you should be able to display authors who published either posts or Custom Post Types (or both).