Support » Everything else WordPress » Author post count from specific category

  • Resolved Gustav

    (@4ever16)


    Author post count from specific category. Whats the code for this?

    This code shows authors all posts count_user_posts( get_the_author_meta('ID')

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

    (@bcworkz)

    Make a new WP_Query for the author’s posts in that category. The post count will be in WP_Query::post_count
    https://developer.wordpress.org/reference/classes/wp_query/

    Thread Starter Gustav

    (@4ever16)

    So i got this code but it prints result 0 why?

    $user_id = get_the_author_meta('ID');
    
    $args = array(
       'author_name'   => $user_id,
       'category_name' => 'categoryname',
    );               
    $wp_query = new WP_Query($args);
    
    $posts = $wp_query->get_posts();
    $my_count = count( $posts );
    echo $my_count;
    Thread Starter Gustav

    (@4ever16)

    Ok so this code works now but why does it prints all posts in that category and never ends printing?

    <?php
    $user_id = get_the_author_meta('ID');
    
    $args = array(
       'author'   => $user_id,
       'category_name' => 'category-slug',
    );               
    $wp_query = new WP_Query($args);
    
    $posts = $wp_query->get_posts();
    $my_count = count( $posts );
    
    echo $my_count;   
    
    ?>
    Thread Starter Gustav

    (@4ever16)

    Solved.

    $user_id = get_the_author_meta('ID');
    
    $args = array(
       'author'   => $user_id,
       'category_name' => 'category_slug_name',
    );    
     
    $my_query = new WP_Query( $args );
    $my_count = $my_query->post_count;
    
    echo $my_count;
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Author post count from specific category’ is closed to new replies.