Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

In Buddypress, show member's total custom post count? WordPress

  • SOLVED

For BuddyPress, how do I display the total amount of a member's published posts for a specific custom post type?

I don't want to include a count of blog posts or any other custom post type.

For example, my custom post type is "recipes". I want to show the total amount of published recipes for an individual member on their profile in the format "Total Recipes: 5".

A [[LINK href="http://wpquestions.com/question/show/id/2758"]]similar previous WP Question attempted an answer for Wordpress[[/LINK]], but the code does not work on BuddyPress.

Answers (2)

2011-10-21

John Cotton answers:

<?php
function count_user_posts_by_type($userid, $post_type='post') {
global $wpdb;
$where = get_posts_by_author_sql($post_type, TRUE, $userid);
$count = $wpdb->get_var( \"SELECT COUNT(*) FROM $wpdb->posts $where\" );
return apply_filters('get_usernumposts', $count, $userid);
}
?>


<?php
if ( $count = count_user_posts_by_type( bp_current_user_id(), 'recipes' ) )
echo "Total Recipes: " . $count; ?>


Jennifer Jermantowicz comments:

I would think this is on the right track. But nothing appears on the profile with:


<?php
if ( $count = count_user_posts_by_type( bp_current_user_id(), 'recipes' ) )
echo "Total Recipes: " . $count; ?>


Also, moved stray " to right place:
function count_user_posts_by_type($userid, $post_type='post') {
global $wpdb;
$where = get_posts_by_author_sql($post_type, TRUE, $userid);
$count = $wpdb->get_var( \"SELECT COUNT(*) FROM $wpdb->posts $where"\ );
return apply_filters('get_usernumposts', $count, $userid);
}


Jennifer Jermantowicz comments:

IT DOES WORK! Thanks John. I missed replacing 'post' in the function with my custom post type name. And that stray " mark is not your fault, but a mistake in the Wordpress Codex. :) For others reference:

In your custom function file :


function count_user_posts_by_type($userid, $post_type='YOUR_CUSTOM_POST_TYPE') {
global $wpdb;
$where = get_posts_by_author_sql($post_type, TRUE, $userid);
$count = $wpdb->get_var( \"SELECT COUNT(*) FROM $wpdb->posts $where"\ );
return apply_filters('get_usernumposts', $count, $userid);
}


And in your template:
<?php if ( $count = count_user_posts_by_type( bp_current_user_id(), 'YOUR_CUSTOM_POST_TYPE' ) ) echo "Total Recipes: " . $count; ?>

2011-10-21

Luis Cordova answers:

can you please post some code of your template to see how you are attempting it?


Jennifer Jermantowicz comments:

I can get a total count of a member's blog posts with:

<?php if (count_user_posts_by_type( bp_current_user_id() ) > 0) echo "Blog posts: " . count_user_posts_by_type( bp_current_user_id() ); ?>

But I don't know how to make that target custom post types instead.


Luis Cordova comments:

search and open the function

count_user_posts_by_type

then paste the code here

it should be a simple change

you should pass the type as argument as second argument perhaps too

so you create a second similar function and inside change the WP_Query