Add custom posts column in users.php – WordPress

One of the greatest features of WordPress is “Custom Post Types” I`m planning on dedicating a whole section of the site to this exact feature, because the information I found when researching about the topic was very poor, and some of the things you might need weren`t even documented at all. The first topic I find very useful is how to add custom posts column in users.php the right way(without editing the core files).

Using the “manage_users_custom_column” hook and “manage_users_columns” filter to include custom posts column in users.php

The whole process will be completed in 2 simple steps:

  1. Add the column title in <thead> and <tfoot>
  2. Get the custom post types count for each user

The first part of the function is pretty simple. You just have to add a filter on “manage_users_columns”. The function provides an array of column names for argument, so adding the column name is as simple as adding an entry in an associative array.In this case the column I`m adding is called contributes.

function contributes($columns) {
    $columns['contributes'] = __('Contributes', 'contributes');
    return $columns;
}
add_filter('manage_users_columns', 'contributes');

The second and more complicated part is calculating the custom posts contributed by each user. In order to do that you need to find the proper action and hook a function to it adding your values to each iteration of the loop. The action you need to hook to is called “manage_users_custom_column”. The function takes three arfuments: $value, $column_name, $user_id.

$value – is the value of the row entry for the category given

$column_name – pretty self-explanatory the name of the column, on which the current iteration is on

$user_id – the id of the user, on which the current iteration is on

To get the custom posts contributed by a current user you`ll need to use query_posts and then the count() PHP function.

function contributes_columns( $value, $column_name, $user_id ) {

    if ( 'contributes' != $column_name )//Replace 'contributes' with the column name from the filter you previously created
        return $value;
    global $wp_query;
    $posts = query_posts('post_type=contribute&author='.$user_id.'&order=ASC&posts_per_page=30');//Replace post_type=contribute with the post_type=yourCustomPostName
    $posts_count = count($posts);
    $posts_count = "<a href='".site_url()."/wp-admin/edit.php?author={$user_id}&post_type=contribute'>{$posts_count}</a>";
    return $posts_count;
}
add_action('manage_users_custom_column', 'contributes_columns', 10, 3);

As simple as that! You`ve added the custom posts column in users.php table.

Installation: The installation of the custom posts column in users.php is as simple as changing the category title and custom post types name in the snippet and pasting it in WordPress functions.php

Adding custom posts column in users.php (Whole code)

function contributes($columns) {
    $columns['contributes'] = __('Contributes', 'contributes');
    return $columns;
}
add_filter('manage_users_columns', 'contributes');
function contributes_columns( $value, $column_name, $user_id ) {

    if ( 'contributes' != $column_name )//Replace 'contributes' with the column name from the filter you previously created
        return $value;
    global $wp_query;
    $posts = query_posts('post_type=contribute&author='.$user_id.'&order=ASC&posts_per_page=30');//Replace post_type=contribute with the post_type=yourCustomPostName
    $posts_count = count($posts);
    $posts_count = "<a href='".site_url()."/wp-admin/edit.php?author={$user_id}&post_type=contribute'>{$posts_count}</a>";
    return $posts_count;
}
add_action('manage_users_custom_column', 'contributes_columns', 10, 3);

Related documentation from the WordPress Codex

WordPress Filters

WordPress Hooks

  • Hey,

    Any ideas on how to then make that column sortable by the number of custom posts that user has made?

    I’ve asked on stackoverflow, hopefully by the time someone else reads this it will have been answered! 🙂

    • Hi Patrick,
      I’d try loading http://tablesorter.com/ in the admin and attaching it to .users 🙂 Hope that helps

    • something like:

      function awordpress_load_scripts($hook) {
      if( $hook != 'users.php' )
      return;

      //jquery.tablesorter.js
      wp_enqueue_script( 'tablesorter', plugins_url( 'js/jquery.tablesorter.js' , dirname(__FILE__) ), array( 'jquery' ) );
      wp_enqueue_script( 'custom-js', plugins_url( 'js/custom.js' , dirname(__FILE__) ), array( 'tablesorter' ) );
      }
      add_action('admin_enqueue_scripts', 'awordpress_load_scripts');

      where in the custom-js you should put something like the following

      jQuery(document).ready(function($) {
      $(".users").tablesorter();
      });

      This should do the trick

      This gist should be more readable