Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added text
Source Link
chowwy
  • 340
  • 5
  • 16

I am using the function below to count posts for users (including custom post types). I am putting together a list of users and want to order them by this count function. The orderby options for the get_users() function are limited, and post_count doesn't include custom post types. How can I order my users using this function?

Function:

function foo_count_posts( $userid, $post_type = '', $post_status = 'publish') {
    global $wpdb;

    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = 'post' OR post_type='foo') AND post_status = '$post_status'"; 
    $counter = $wpdb->get_var($query); 
    
    return apply_filters( 'get_usernumposts', $counter, $userid );  
}

Then I call the function to get the count:

echo foo_count_posts($user_id);

I am using the function below to count posts for users (including custom post types). I am putting together a list of users and want to order them by this count function. The orderby options for the get_users() function are limited, and post_count doesn't include custom post types. How can I order my users using this function?

function foo_count_posts( $userid, $post_type = '', $post_status = 'publish') {
    global $wpdb;

    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = 'post' OR post_type='foo') AND post_status = '$post_status'"; 
    $counter = $wpdb->get_var($query); 
    
    return apply_filters( 'get_usernumposts', $counter, $userid );  
}

Then I call the function to get the count:

echo foo_count_posts($user_id);

I am using the function below to count posts for users (including custom post types). I am putting together a list of users and want to order them by this count function. The orderby options for the get_users() function are limited, and post_count doesn't include custom post types. How can I order my users using this function?

Function:

function foo_count_posts( $userid, $post_type = '', $post_status = 'publish') {
    global $wpdb;

    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = 'post' OR post_type='foo') AND post_status = '$post_status'"; 
    $counter = $wpdb->get_var($query); 
    
    return apply_filters( 'get_usernumposts', $counter, $userid );  
}

Then I call the function to get the count:

echo foo_count_posts($user_id);
Source Link
chowwy
  • 340
  • 5
  • 16

Order users by number of posts (includes custom post type)

I am using the function below to count posts for users (including custom post types). I am putting together a list of users and want to order them by this count function. The orderby options for the get_users() function are limited, and post_count doesn't include custom post types. How can I order my users using this function?

function foo_count_posts( $userid, $post_type = '', $post_status = 'publish') {
    global $wpdb;

    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = 'post' OR post_type='foo') AND post_status = '$post_status'"; 
    $counter = $wpdb->get_var($query); 
    
    return apply_filters( 'get_usernumposts', $counter, $userid );  
}

Then I call the function to get the count:

echo foo_count_posts($user_id);