Hi,
I am trying to find a solution to show BP Activity post count inside the user coverphoto.
Is there a solution that you have already provided on this subject? I couldn’t find any posts in the forum but I could find some code in the internet and I wanted to ask if this code will be compatible with BP Nouveau?
Function:
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 then to call the function inside the template:
<?php if ( $count = count_user_posts_by_type( bp_current_user_id(), 'YOUR_CUSTOM_POST_TYPE' ) ) echo "Total Recipes: " . $count; ?>
Would this make sense?
Thanks for your time.
Regards
DanielHi Daniel,
Thank you for the question.
It seems good to me except the backslash part. You should remove the two backslash from the get_var method call.Regards
BrajeshThank you Daniel.
I hope it goes well.Regards
BrajeshHi Brajesh,
The code is actually not working. After inserting it into the child’s function.php then when loading the website it says “There has been a critical error on this website.” and the website is not loading anymore.
Did I miss inserting a syntax in the beginning of the function code? I have just copy-pasted the code above into the functions.php.
Thanks for the hint.
Regards
DanielPlease share the code you have used(exact copy).
Thank you
BrajeshHi Brajesh,
I had used this exact code in child theme functions.php
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 then for the Buddypress member-header.php I have added this code:
<?php if ( $count = count_user_posts_by_type( bp_current_user_id(), 'YOUR_CUSTOM_POST_TYPE' ) ) echo "Total Recipes: " . $count; ?>
Thank you.
Regards
Daniel- This reply was modified 3 years, 9 months ago by Daniel.
Hi Ravi,
Actually, I only have inserted the first code into functions.php and the code to call the function I haven’t inserted yet. But then when I uploaded the new functions.php, the website did not load anymore. It was a white screen with the following error message: “There has been a critical error on this website.” Therefore I guess something is wrong with the first code:
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); }
By the way, shouldn’t it say in the beginning “add function” instead of only “function”?
Thanks for your time.
Regards,
Daniel
You must be logged in to reply to this topic.