0

I am trying to get the number of posts by a user including all post status (draft, pending review & published).

This will be used for a rule. If a user has more than or equal to X number of posts on a post type, they will see a customized text.

Found the following but that only counts the published posts.

$userID = get_current_user_id();
echo 'Number of posts published by user: ' . count_user_posts( $userID , "books"  );
0

Sounds very simple as querying for posts and iterating through the results and working with the post status.. Just make sure to add 'post_status' => 'any' to the query args so as to include more statuses beside the published:

$args = array(
    'author' => 1, // user ID here
    'posts_per_page' => -1, // retrieve all
    'post_type' => 'post', // post type (change to your PT)
    'post_status' => 'any' // any status
);

$posts = get_posts( $args );

$drafts = $pendings = $published = array();

if ( ! empty( $posts ) ) :;

    foreach ( $posts as $post ) {
        switch ( $post->post_status ) {
            case 'draft':
                $drafts[] = $post;
                break;
            case 'pending':
                $pendings[] = $post;
                break;
            case 'published':
                $published[] = $post;
                break;
            default:
                break;
        }
    }

endif;

echo var_dump( 'drafts', $drafts, 'pending', $pendings, 'published', $published ); // or print_r

Hope that helps.

This will be used for a rule. If a user has more than or equal to X number of posts on a post type, they will see a customized text.

An example usage: if ( count( $drafts ) >= (int) X ) { # Hey!! }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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