How to show custom post type count in the users admin page

There’s a post count in the user page of the WordPress admin area. I have a custom post type events. Is there some hook I can use to add my event count to the post count, or else to add a column for the event count?

Related posts

2 comments

  1. There are 2 pretty undocumented hooks 'manage_users_custom_column' and 'manage_users_columns' that you can use for the scope.

    They works in same manner of 'manage_posts_custom_column' and 'manage_posts_columns' that are better documented, see Codex.

    So you can do something like this:

    /*
     * Add Event Column 
     */
    function users_events_column( $cols ) {
      $cols['user_events'] = 'Events';   
      return $cols;
    }
    
    /*
     * Print Event Column Value  
     */ 
    function user_events_column_value( $value, $column_name, $id ) {
      if( $column_name == 'user_events' ) {
        global $wpdb;
        $count = (int) $wpdb->get_var( $wpdb->prepare(
          "SELECT COUNT(ID) FROM $wpdb->posts WHERE 
           post_type = 'events' AND post_status = 'publish' AND post_author = %d",
           $id
        ) );
        return $count;
      }
    }
    
    add_filter( 'manage_users_custom_column', 'user_events_column_value', 10, 3 );
    add_filter( 'manage_users_columns', 'users_events_column' );
    

    only be sure to use the correct post type slug in the SQL query inside user_events_column_value funcion

  2. here’s an improvment to @gmazzap’s answer, including links to post list:

    /*
     * Add Location (business) Column to user screen
     */
    function bayadaim_users_location_column( $cols ) {
      $cols['user_locations'] = 'YOUR_COLUMN_NAME';  
      return $cols;
    }
    
    /*
     * Print Event Column Value  
     */ 
    function bayadaim_users_location_column_value( $value, $column_name, $id ) {
      if( $column_name == 'user_locations' ) {
        global $wpdb;
        $count = (int) $wpdb->get_var( $wpdb->prepare(
          "SELECT COUNT(ID) FROM $wpdb->posts WHERE 
           post_type = 'YOUR_POST_TYPE' AND post_status = 'publish' AND post_author = %d",
           $id
        ) );
        $link = '<a href="' . admin_url() . 'edit.php?post_type=location' . '&author=' . $id . '" title="Posts location">' . $count . '</a>';
    
        $return = ($count > 0) ? $link : $count;
    
        return $return;
      }
    }
    
    add_filter( 'manage_users_custom_column', 'bayadaim_users_location_column_value', 10, 3 );
    add_filter( 'manage_users_columns', 'bayadaim_users_location_column' );
    

Comments are closed.