565 posts WeNotMe
  • Has been part of the Envato Community for over 5 years
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has referred 1+ members
  • Had an item that was trending
+4 more
codeex
says

Hi,

Default WordPress functions get_users(‘orderby=post_count’); only orders users with the count of the posts they made. Any idea how to modify this to support in cpt as well?

here’s my code


$args = array( 'orderby' => 'post_count', 'order' => 'DESC', 'role' => 'contributor', 'number' => $homeland_agent_limit );
$homeland_agents = get_users ( $args );

foreach ($homeland_agents as $homeland_user) :
    global $wpdb;
    $homeland_post_author = $homeland_user->ID;
    $homeland_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'homeland_properties' AND post_status = 'publish' AND post_author = %d", $homeland_post_author ) );
?>
    <li class="clear">
        <?php 
               echo $homeland_user->display_name; 
               echo intval($homeland_count); 
       ?>
    </li>    
<?php
endforeach;

anyone here has a fix?

thanks

350 posts Gambit
  • Has been part of the Envato Community for over 5 years
  • Has referred 10+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 100+ items on Envato Market
+6 more
bfintal
says
565 posts WeNotMe
  • Has been part of the Envato Community for over 5 years
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has referred 1+ members
  • Had an item that was trending
+4 more
codeex
says

Maybe http://codex.wordpress.org/Function_Reference/wp_count_posts can help?

thanks but this will only count the total of property posts.. current code works like this

Mark (20)
Jane (100)
John (50)

what I want to achieve is this

Jane (100)
John (50)
Mark (20)

post_count pulls from cpt not posts..

137 posts
  • Had an item that became a weekly top seller
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+6 more
butterflymedia
says

I’m also interested in this, if anyone knows a solution.

136 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Joined an Envato Football competition
+3 more
Konnessa
says

Hi,

I am a little unclear as to what you are trying to achieve – Are you asking for the query to be ordered by total posts including custom post types, or total posts of a certain custom post type?

You can try to replace the where clause of the query by hooking to pre_user_query. 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');
137 posts
  • Had an item that became a weekly top seller
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+6 more
butterflymedia
says

Hi,

I am a little unclear as to what you are trying to achieve – Are you asking for the query to be ordered by total posts including custom post types, or total posts of a certain custom post type?

You can try to replace the where clause of the query by hooking to pre_user_query.

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

I have already tried that and it’s not working.

I have this page – http://cghubs.com/our-artists/ – and I want to sort users by the number of custom post types they published.

136 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Joined an Envato Football competition
+3 more
Konnessa
says

Hi,

There is always the option of using wpdb to obtain back the count and userids.

$query = <<<EOF
SELECT post_type,post_author,COUNT(*) AS post_count
FROM {$wpdb->posts}
        WHERE post_type IN ('post','my_custom_post_type')
        AND post_status='publish'
        GROUP BY post_type, post_author
EOF;

$posts = $wpdb->get_results($sql);

You can then iterate through this and obtain the relevant user details.

137 posts
  • Had an item that became a weekly top seller
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+6 more
butterflymedia
says

Well, actually I revisited the first function and replaced “post” with ”’post’”. It seems that it was an old function and WordPress updated the query in the mean time. So, ‘post’ should be quoted.

It works, check out the page – http://cghubs.com/our-artists/

by
by
by
by
by
by