3

Im trying to order by post_count DESC but custom_post_type not working

function post_count_type($role, $number) {
$args = array(
    'post_type'     => array( 'custom_post_type' ),
    'orderby'       => 'post_count',
    'order'         => 'DESC',
    'role'          => $role,
    'number'        => $number
);
$users = get_users( $args );
$the_query = new WP_Query( $args );
    foreach ($users as $user) {
    echo 'html here...';
    }}

My function work for post_type : POST but not for custom post_type. Sorry for my english.

2
  • 1
    What is post_count? Each post should equate to 1 post, categories would have multiple posts assigned to there where a post_count would make sense. Would you happen to mean the order that you can assign under Page Attributes metabox?
    – Howdy_McGee
    Nov 3, 2019 at 22:16
  • The post_count is for user. I want order user by whoever posted the most article on custom_post_type. Again sorry for my english hope you understand.
    – mems
    Nov 4, 2019 at 7:50

1 Answer 1

6

You are using wrong approach

You are using the wrong approach for what you are trying to achieve. You are using the same arguments for both WP_Query and WP_User_Query (get_users eventually translates to WP_User_Query). The problem is that WP_Query doesn't support orderby parameter post_count as one post is always equal to one post and WP_User_Query doesn't support post_type as the user doesn't have post type assigned to themselves.

This can't be achieved using standard functions

In WP_User_Query it's hardcoded that for user posts count, it looks only for post_type post. To achieve what you are trying to, you need to write a custom database query.

A custom solution

A standalone function to achieve this might look like this (please note I haven't tested it so there might be a SQL typos or smth).

function get_users_by_post_count( $post_type = 'post' ) {
    global $wpdb;

    $users = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT {$wpdb->users}.ID, p.post_count
            FROM {$wpdb->users}  
            LEFT JOIN (
                SELECT post_author, COUNT(*) AS post_count 
                FROM {$wpdb->posts} WHERE post_type = %s
                GROUP BY post_author
            ) p ON {$wpdb->users}.id = p.post_author 
            ORDER BY p.post_count",
            $post_type
        )
    );

    return $users;
}

To use it simply call it like:

$users = get_users_by_post_count( 'custom_post_type' );

if ( ! empty( $users ) ) {
   foreach ( $users as $user ) {
       // there you have user ID and post_count
       echo $user->ID . ' has ' . $user->post_count . ' posts';
    }
}
5
  • It looks like a core bug (at least from a first look) to have hardcoded get_posts_by_author_sql( 'post' ); when ordering by post_count, since the post type should be adjustable via the has_published_posts (and blog_id non-zero). ps: there seems to be missing return $users; in get_users_by_post_count(). Adding a limit would be recommended for large number of users.
    – birgire
    Nov 4, 2019 at 15:32
  • 2
    Thanks @birgire I've updated the function to return the result. Yes, it's impossible due to hardcoded post type in get_posts_by_author_sql it's an easy fix and I'm going to open a ticket in WP community to get it solved (never did this before, su can't guarantee it will be successful :) ) Nov 4, 2019 at 16:43
  • Sounds good, would be nice to have the ticket # posted here ;-)
    – birgire
    Nov 4, 2019 at 16:51
  • Thank you very much, it helped me. Have a nice day 👌
    – mems
    Nov 4, 2019 at 18:13
  • @birgire long time no see, haha. core.trac.wordpress.org/ticket/49418 Feb 12, 2020 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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