Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to Count Custom Post Types in Nav Tabs?


  • 3T_MJ
    Participant

    @3t_mj

    I added a Post Tab to my Buddypress Navigation Tab Menu which displays the latest Posts and Custom Posts for each user.

    Now I want to add the total number of all Posts (standard wordpress posts and CPT posts) but only standard posts get counted. What am I doing wrong?

    function bpck_post_profile_setup_nav() {
    	global $bp;
    	$parent_slug = 'post';
    	$child_slug = 'post_sub';
    
        $post_count = count_user_posts(  bp_displayed_user_id(), array('video','discography','post' ) );
    
    	bp_core_new_nav_item( array(
    	'name' => 'My Posts <span>'.$post_count.'</span>',
    	'slug' => $parent_slug,
    	'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/',
    	'screen_function' => 'profile_page_show_screen',
    	'position' => 40,
    	'default_subnav_slug' => $child_slug
    	) );
    	}
    
    	function profile_page_show_screen() {
    	add_action( 'bp_template_content', 'profile_post_page_screen_content' );
    	bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    	}
    
    	function profile_post_page_screen_content() {
    		do_action( my_profile_post);
    	}
    add_action( 'bp_setup_nav', 'bpck_post_profile_setup_nav' );

    What’s even more confusing if I add

    function my_custom_userpostcount() {
    echo '<span>' . count_user_posts( bp_displayed_user_id(), array('video','discography','post' ) ) . ' Posts</span>';
    
    }
    add_action( 'bp_before_member_header_meta', 'my_custom_userpostcount');

    to the user header, the total count number is correct, it counts all post types in the array.

    Don’t know what I’m doing wrong.

Viewing 11 replies - 1 through 11 (of 11 total)

  • Henry Wright
    Moderator

    @henrywright

    The only difference is the hooks you’re using:

    • bp_setup_nav
    • bp_before_member_header_meta

    I expect the problem might be related to when bp_setup_nav is executed?


    3T_MJ
    Participant

    @3t_mj

    @henrywright but apparently I need to use bp_setup_nav to add a navigation tab. I don’t understand why bp_setup_nav doesn’t let me to count custom post types.


    Henry Wright
    Moderator

    @henrywright

    @henrywright but apparently I need to use bp_setup_nav to add a navigation tab

    Yes, that’s right, you do.

    This is one of those situations where you seem to be doing everything right but a bit of digging is needed to see what’s actually wrong.


    danbp
    Moderator

    @danbp

    What if you add a priority ? ie. add_action( 'bp_setup_nav', 'bpck_post_profile_setup_nav', 15 );


    3T_MJ
    Participant

    @3t_mj

    @danbp unfortunately this doesn’t make any difference. I tried different priorities but custom post type posts still don’t get counted.


    danbp
    Moderator

    @danbp

    Ok. Can you check bp_activities table in DB. When you publish a dicography, does it appear as type (give exact name) or only as “new_blog_post” ?


    shanebp
    Moderator

    @shanebp

    I don’t know why it would work in bp_before_member_header_meta.

    I usually use a direct call for such counts.
    Try something like:

    function 3tmj_count_profile( $user_id = 0 ) {
    	global $wpdb;
    
    	if ( empty( $user_id ) )
    		$user_id = bp_displayed_user_id();
    
    	return $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_author = $user_id AND post_type IN { 'video','discography','post' }  AND post_status = 'publish'" );
    
    }

    Then implement – note the parent url:

    function bpck_post_profile_setup_nav() {
    
    	$parent_slug = 'post';
    	$child_slug = 'post_sub';
    
    	$count  = 3tmj_count_profile();
    	
    	$class  = ( 0 === $count ) ? 'no-count' : 'count';
    	
    	$name = sprintf( __( 'My Posts <span class="%s">%s</span>', 'your-lang-domain' ), esc_attr( $class ), number_format_i18n( $count ) );
    
    	bp_core_new_nav_item( array(
    		'name' => $name,
    		'slug' => $parent_slug,
    		'parent_url' => bp_displayed_user_domain() . $parent_slug . '/',
    		'screen_function' => 'profile_page_show_screen',
    		'position' => 40,
    		'default_subnav_slug' => $child_slug
    	) );
    }

    danbp
    Moderator

    @danbp

    I tested your code and CPT & Posts counts are both correct (on tab and profile).

    How did you implement CPT ? Try to test with the snippet given here:

    Post Types Activities


    3T_MJ
    Participant

    @3t_mj

    @danbp: I just want to display the total number of the given post types combined. I haven’t implemented it to the activities yet but to my understanding it should be displayed indepently from the activity setup, shouldn’t it? It works for the header correctly.

    @shanebp: I haven’t tested your version yet. What do you mean with your-lang-domain? My website is only written in english, is it necessary then?


    Patrick
    Participant

    @zaanmedia

    I’m also trying to do the same thing! Did you ever find the solution to make it work?


    Patrick
    Participant

    @zaanmedia

    @shanebp

    My code is the following and i got an error500 with your code. Did i forgot something to do i’m confused. No feedback from anyone if this solution worked or not?

    function 3tmj_count_profile( $user_id = 0 ) {
    	global $wpdb;
    
    	if ( empty( $user_id ) )
    		$user_id = bp_displayed_user_id();
    
    	return $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_author = $user_id AND post_type IN { 'wpcm_vehicle' }  AND post_status = 'publish'" );
    
    }
    
    function profile_tab_advertenties() {
        	global $bp;
    
    	$count = 3tmj_count_profile();
    	
    	$class = ( 0 === $count ) ? 'no-count' : 'count';
    	
    	$name = sprintf( __( 'My Posts <span class="%s">%s</span>', 'buddypress-meets-wp-car-manager' ), esc_attr( $class ), number_format_i18n( $count ) );
    
    	//$name = sprintf( __( 'Advertenties <span class="%s">%s</span>', 'buddypress-meets-wp-car-manager' ), esc_attr( $class ), number_format_i18n( $count ) );
    	//$name = 'Advertenties <span>' . count_user_posts( bp_displayed_user_id(), array( 'wpcm_vehicle', 'post' ) ) . '</span>';
    
       	bp_core_new_nav_item( array( 
    		'name' => $name,
    		'slug' => __( 'advertenties', 'buddypress-meets-wp-car-manager' ), 
            	'screen_function' => 'advertenties_screen', 
            	'position' => 30,
    	    	'parent_url'      => bp_displayed_user_domain()  . __( '/advertenties/', 'buddypress-meets-wp-car-manager' ) ,
    	    	'parent_slug'     => $bp->profile->slug,
    		'default_subnav_slug' => __( 'advertenties', 'buddypress-meets-wp-car-manager' ), 
        ) );
    }
    add_action( 'bp_setup_nav', 'profile_tab_advertenties', 10);
Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar