WordPress count user posts of different custom post types

I’ve been looking for a way to count the number of custom posts a user has created, and was able to do it using this snippet:

<?php

    $userid = get_current_user_id();

    function count_user_posts_by_type($userid, $post_type = 'foo_type', $post_status = 'publish') {

    global $wpdb; 
    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND post_type = '$post_type' AND post_status = '$post_status'"; 
    $count = $wpdb->get_var($query); 
    return apply_filters('get_usernumposts', $count, $userid);

} ?>

And then echo the result with:

Read More
<?php echo count_user_posts_by_type($userid); ?>

My question: Above code outputs only the count of the custom post type “foo_type”. If I have two custom post types – “foo_type” and “bar_type” – how do I change this code so that it returns the count of both of them rather than just the count of “foo_type”?

Related posts

Leave a Reply

3 comments

  1. Add the second post_type to the query:

    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = '$post_type' OR post_type='$post_type_2') AND post_status = '$post_status'"; 
    
  2. Try custom function given below

    function my_count_posts_by_user($post_author=null,$post_type=array(),$post_status=array()) {
        global $wpdb;
    
        if(empty($post_author))
            return 0;
    
        $post_status = (array) $post_status;
        $post_type = (array) $post_type;
    
        $sql = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND ", $post_author );
    
        //Post status
        if(!empty($post_status)){
            $argtype = array_fill(0, count($post_status), '%s');
            $where = "(post_status=".implode( " OR post_status=", $argtype).') AND ';
            $sql .= $wpdb->prepare($where,$post_status);
        }
    
        //Post type
        if(!empty($post_type)){
            $argtype = array_fill(0, count($post_type), '%s');
            $where = "(post_type=".implode( " OR post_type=", $argtype).') AND ';
            $sql .= $wpdb->prepare($where,$post_type);
        }
    
        $sql .='1=1';
        $count = $wpdb->get_var($sql);
        return $count;
    } 
    

    And then echo the result with:

    <?php echo my_count_posts_by_user($userid , array('posttype1' , 'posttype2')); ?>
    

    Please look on url given below..

    https://wordpress.stackexchange.com/questions/43549/count-user-posts-by-user-id-post-type-and-post-status

    thanks

  3. $args = array(
        'post_type'=> 'page',
        'author'    => '1',
        'post_staus'=> 'publish',
        'posts_per_page' => -1
    );
    echo custom_count_post_by_author($args);
    function custom_count_post_by_author($args){
        query_posts( $args );
        $count = 0;
        if ( have_posts() ) :
             while ( have_posts() ) : the_post();
                $count++;
             endwhile; 
        endif;
        wp_reset_query();
        return $count;
    }
    

    You can pass any Arguments in $args which support query_posts