607 posts WeNotMe
  • 5 Years of Membership
  • Elite Author
  • Author Level 7
  • Trendsetter
+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
  • 5 Years of Membership
  • Affiliate Level 2
  • Author Level 7
  • Collector Level 4
+6 more
bfintal
says
607 posts WeNotMe
  • 5 Years of Membership
  • Elite Author
  • Author Level 7
  • Trendsetter
+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..

157 posts
  • Weekly Top Seller
  • Featured Author
  • Author Level 6
  • 5 Years of Membership
+6 more
butterflymedia
says

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

136 posts
  • Affiliate Level 1
  • Author Level 3
  • Collector Level 4
  • Football Player
+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');
157 posts
  • Weekly Top Seller
  • Featured Author
  • Author Level 6
  • 5 Years of Membership
+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
  • Affiliate Level 1
  • Author Level 3
  • Collector Level 4
  • Football Player
+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.

157 posts
  • Weekly Top Seller
  • Featured Author
  • Author Level 6
  • 5 Years of Membership
+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