Skip to:
Content
Pages
Categories
Search
Top
Bottom

Run WP functions in bp-custom.php


  • marcelo2605
    Participant

    @marcelo2605

    I created a bp-custom.php file inside plugins folder and need to run count_user_posts() inside bp_setup_nav hook. It is possible?

    
    add_action( 'bp_setup_nav', function() {
        global $bp;
    
        $post_count = count_user_posts( $bp->displayed_user->id, 'drawings' );
    
        $args = array(
    		'name' => $post_count,
    		'slug' => 'drawings',
    		'default_subnav_slug' => 'drawings',
    		'position' => 100,
    		'screen_function' => 'redirect_user_to_author_page'
        );
    
        bp_core_new_nav_item( $args );
    }, 99 );
    
Viewing 2 replies - 1 through 2 (of 2 total)

  • shanebp
    Moderator

    @shanebp

    $bp->displayed_user->id is only available if you are viewing a profile page.

    And it is better to remove the global $bp and use bp_displayed_user_id().

    Try adding a check to see if you are viewing a profile page:

    if ( bp_is_user() ) { 
    
       $post_count = count_user_posts( bp_displayed_user_id(), 'drawings' );
    
       // etc
    }

    marcelo2605
    Participant

    @marcelo2605

    Thanks!

    But there is still a problem: in bp-custom.php, count_user_posts is not working with CPTs. And I’m using latest version of WP. You known why?

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar