288 posts WeNotMe
  • Has been part of the Envato Community for over 4 years
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has referred 1+ members
  • Elite Author: Sold more than $75,000 on Envato Market
+3 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
+4 more
bfintal says
288 posts WeNotMe
  • Has been part of the Envato Community for over 4 years
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has referred 1+ members
  • Elite Author: Sold more than $75,000 on Envato Market
+3 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..

118 posts
  • Has been part of the Envato Community for over 4 years
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+5 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');
118 posts
  • Has been part of the Envato Community for over 4 years
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+5 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.

118 posts
  • Has been part of the Envato Community for over 4 years
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+5 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/

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by