I’m trying to figure out how to hook into the /wp-admin/users.php manage page to
create custom columns for showing the number of posts users have for the custom post types on WPHonors.com.

I created a trac ticket for this but @nacin explained why it’s a job more for a plugin to do instead.

I have been unable to find a way to manipulate the output of the users
table, so I can add custom columns for CPTs post counts for each user. And
that may have something to do with the question @nacin asked, what would the
post count numbers link to. For the current ‘post’ post count a user has, it
links to the post manage page, showing all posts for that user
(/wp-admin/edit.php?author=%author_id%).

If I were to link it somewhere, it would be to:

/wp-admin/edit.php?post_type=%post_type%&author=%author_id%

If that were even somehow possible, I guess. But I don’t even necessarily need to link it to anywhere. I mostly want to just show CPT post counts for each person, having 600 users and a combined total of 300+ posts across 4 custom post types. Admins are only one who can submit 'post' posts, so that column in the user’s page is useless.

4 s
4

Here’s an expansion of Mike’s tutorial answer. I added links to the types listed so you can click on one and be taken right to a listing of all the posts in that type for that author, which required an additional variable for $counts and some extra output for $custom_column[]

add_action('manage_users_columns','yoursite_manage_users_columns');
function yoursite_manage_users_columns($column_headers) {
    unset($column_headers['posts']);
    $column_headers['custom_posts'] = 'Assets';
    return $column_headers;
}

add_action('manage_users_custom_column','yoursite_manage_users_custom_column',10,3);
function yoursite_manage_users_custom_column($custom_column,$column_name,$user_id) {
    if ($column_name=='custom_posts') {
        $counts = _yoursite_get_author_post_type_counts();
        $custom_column = array();
        if (isset($counts[$user_id]) && is_array($counts[$user_id]))
            foreach($counts[$user_id] as $count) {
                $link = admin_url() . "edit.php?post_type=" . $count['type']. "&author=".$user_id;
                // admin_url() . "edit.php?author=" . $user->ID;
                $custom_column[] = "\t<tr><th><a href={$link}>{$count['label']}</a></th><td>{$count['count']}</td></tr>";
            }
        $custom_column = implode("\n",$custom_column);
        if (empty($custom_column))
            $custom_column = "<th>[none]</th>";
        $custom_column = "<table>\n{$custom_column}\n</table>";
    }
    return $custom_column;
}

function _yoursite_get_author_post_type_counts() {
    static $counts;
    if (!isset($counts)) {
        global $wpdb;
        global $wp_post_types;
        $sql = <<<SQL
        SELECT
        post_type,
        post_author,
        COUNT(*) AS post_count
        FROM
        {$wpdb->posts}
        WHERE 1=1
        AND post_type NOT IN ('revision','nav_menu_item')
        AND post_status IN ('publish','pending', 'draft')
        GROUP BY
        post_type,
        post_author
SQL;
        $posts = $wpdb->get_results($sql);
        foreach($posts as $post) {
            $post_type_object = $wp_post_types[$post_type = $post->post_type];
            if (!empty($post_type_object->label))
                $label = $post_type_object->label;
            else if (!empty($post_type_object->labels->name))
                $label = $post_type_object->labels->name;
            else
                $label = ucfirst(str_replace(array('-','_'),' ',$post_type));
            if (!isset($counts[$post_author = $post->post_author]))
                $counts[$post_author] = array();
            $counts[$post_author][] = array(
                'label' => $label,
                'count' => $post->post_count,
                'type' => $post->post_type,
                );
        }
    }
    return $counts;
}

Leave a Reply

Your email address will not be published. Required fields are marked *