Quantcast
Viewing all articles
Browse latest Browse all 10

Get post count for the user by their post type in wordpress and bbpress 2.0



We use count_user_posts function to get count of the total post for the user whose ID is passed to this function. We can use this function as:

  1.  <?php count_user_posts( $userid ); ?>

You can find this function in wp-includes/user.php.


But if we want to get the post count by their post type ( such as Post, Revision etc.), then you can use simple function derived from count_user_posts(), with post_type support added as second parameter. You can write this function in wp-includes/user.php.

  1. function count_user_posts_by_type($userid, $post_type)
  2. {
  3. global $wpdb;
  4.   $where = get_posts_by_author_sql($post_type, TRUE, $userid);
  5.  $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts $where" );
  6.   return apply_filters('get_usernumposts', $count, $userid);
  7. }


How to use this function:

You can use this function by passing $userid and $post_type like as:

  1. <?php echo 'Number of Article: '. count_user_posts_by_type($user_ID,'post');?>


How to use this function in bbpress 2:

If you are using bbpress 2.0 in your wordpress blog and you want to display number of the post, thread and reply on the user profile page. Then you can use the count_user_posts_by_type() in userddetails.php in your bbpress plugin directory like as :

  1. <div id="author-description">
  2.     <h1><?php printf( __( 'About %s', 'bbpress' ), bbp_get_displayed_user_field( 'display_name' ) ); ?></h1>
  3.  
  4.    <?php echo bbp_get_displayed_user_field( 'description' ); ?>
  5.  
  6.   </div><!– #author-description –>
  7. <div style="margin-top:10px;margin-bottom:=10px;font-size:11px;margin-left:70px;">
  8. <li>Name:<?php echo bbp_get_displayed_user_field( 'display_name' ); ?></li>
  9.  
  10. <li><?php echo 'Number of Article: ' . count_user_posts_by_type( bbp_get_displayed_user_field( 'ID'),'post'); ?></li>
  11. <li><?php echo 'Number of Thread started: ' . count_user_posts_by_type( bbp_get_displayed_user_field( 'ID'),'topic'); ?></li>
  12. <li><?php echo 'Number of Reply: ' . count_user_posts_by_type( bbp_get_displayed_user_field( 'ID'),'reply'); ?></li>
  13.  
  14. </div>

Image may be NSFW.
Clik here to view.


Viewing all articles
Browse latest Browse all 10

Trending Articles