BuddyDev

Search

Buddypress Post Count to show on User BP page

  • Participant
    Level: Master
    Posts: 497
    Daniel on #36183

    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
    Daniel

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #36192

    Hi 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
    Brajesh

  • Participant
    Level: Master
    Posts: 497
    Daniel on #36221

    Hi Brajesh,

    Thank you for having a look at it, that’s awesome!

    I will try it out 🙂

    Thanks again.

    Regards
    Daniel

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #36233

    Thank you Daniel.
    I hope it goes well.

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 497
    Daniel on #36242

    Hi 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
    Daniel

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #36243

    Please share the code you have used(exact copy).

    Thank you
    Brajesh

  • Participant
    Level: Master
    Posts: 497
    Daniel on #36246

    Hi 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, 10 months ago by Daniel.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #36255

    Hello Daniel,

    Try replacing ‘YOUR_CUSTOM_POST_TYPE’ with your recipe posttype and let me know if it helps or not. Please share the error if occures.

    Regards
    Ravi

  • Participant
    Level: Master
    Posts: 497
    Daniel on #36256

    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

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #36257

    Hello Daniel,

    Thank you for the acknowledgement. But I have tested above mentioned code and it is working fine for me.
    Please try debugging in WordPress there might be some other issue going on.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: not resolved