• Resolved nathanrbiz25

    (@nathanrbiz25)


    Hi,

    In the backend under the users area where there is a total number of posts created by that user is there a way to also include custom post types in that total?

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m going to hesitantly say yes, but I don’t know off the top of my head. I’d have to check on how to, if it is indeed possible. It’s not a setting we have for CPTUI though, so it’s going to be a custom modification.

    Thread Starter nathanrbiz25

    (@nathanrbiz25)

    I found this which does the job:

    //show custom posts types in user area
    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>{$count[‘label’]}</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;
    }

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Whatever gets the job done.

    For possible use for other people, can you provide a link to where you originally got the code?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Count Custom Posts In User Area’ is closed to new replies.