• Resolved apaunganhote

    (@apaunganhote)


    Hello,

    I want to show user’s post count from specific category. Currently, I can only be able to query all posts. My code is like this

    <?php $userpost_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='post' AND post_author = '".$curauth->ID."'");?>
    <?php echo "<span>Total post: </b></span>".$userpost_count.""?>

    I know that, I need to join two table which is post table and term_relationships, but i don’t know how to get it. Please kindly help me with that. Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • More that you asked but see if this sheds light:

    <?php
    //displays users along with count of posts each user has belonging to category 3
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->user_id);
        $userposts = get_posts('cat=3&showposts=-1&author='.$bloguser->user_id);
        $count=count($userposts);
        echo '<p>User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . ', posts in category 3: ' . $count . '</p>';
      }
    }
    ?>
    Thread Starter apaunganhote

    (@apaunganhote)

    Hello Michael,

    Thank you again for your support. Really appreciate it. Seems like, my post is misleading or not enough information. The one I was trying to place post count is at author page. I want to show that author has how many post in specific category.

    When I pasted to your code at that page, it display alot of results.

    I asked the same post as stackoverflow and get the result. Here is the snippet for other people who want to get like me also.

    <?php $userpost_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.term_id = CATEGORY_ID
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->posts.post_status = 'publish'
    AND post_author = '".$curauth->ID."'");?>
    Tosh

    (@xberserker)

    Thanks apaunganhote that worked for my needs ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘User’s Post count from specific category’ is closed to new replies.