Support » Developing with WordPress » Counting user post count, including all post_status

  • Resolved beyond55



    I need to check the users post count with a function. I already have this, the only problem though, is that it only counts posts with the post status of ‘published’.

    $user_post_count = count_user_posts( $the_user_id, 'property' );
      if ( is_page( array( 'site1', 'site2' ) ) && $user_post_count == "1" )

    How do I count the total post count, including ‘draft’ and ‘pending’ etc.?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    Make a custom WP_Query and include the ‘post_status‘ argument which is an array of all statuses you want to include in the count. Naturally include the ‘author’ argument with $the_user_id as well. Once instantiated with those arguments, the count will be available in WP_Query::post_count.

    Thank you for your answer.
    I’ve been trying to make that work, but the value keeps saying NULL.

    add_action( 'template_redirect', 'redirect_to_specific_page' );
    function redirect_to_specific_page() {
    $current_user = wp_get_current_user();
    	if (empty($the_user_id)) {
    		$the_user_id = $current_user->ID;
    $args = array(
        'property_type' => 'property',
        'author'    => $the_user_id,
        'post_staus'=> 'any'
    function custom_count_post_by_author($args){
        query_posts( $args );
        $count = 0;
        if ( have_posts() ) :
             while ( have_posts() ) : the_post();
        return $count;
        var_dump(is_page('site1'), $count);
      if ( is_page( 'site1' ) && $count == "1" ) {
        wp_redirect( 'site2', 301 ); 

    Maybe you can tell me what I’m doing wrong?

    Moderator bcworkz


    Because $count in the custom_count_post_by_author() function is local to the function. Outside of the function it has no meaning.

    Add this before you var_dump to get an actual count:
    $count = custom_count_post_by_author($args);

    Aha, it is now working. Thank you very much 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Counting user post count, including all post_status’ is closed to new replies.