Showing User's Post Counts by Custom Post Type in the Author.php?

i see code in link how to showing User's Post Counts by Custom Post Type in the Admin's User List, but i want

i see code in link how to showing User's Post Counts by Custom Post Type in the Admin's User List, but i want show posts count in author.php

Showing User's Post Counts by Custom Post Type in the Admin's User List?

is show in users.php like image:

But i want show posts count like image in author.php, please help me, thanks.

i see code in link how to showing User's Post Counts by Custom Post Type in the Admin's User List, but i want show posts count in author.php

Showing User's Post Counts by Custom Post Type in the Admin's User List?

is show in users.php like image:

But i want show posts count like image in author.php, please help me, thanks.

Share Improve this question asked Oct 15, 2019 at 10:07 Duy HữuDuy Hữu 195 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Here I've modified the code from URL you've given.

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;
}
$user_id = get_current_user_id();
$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>";
        echo $count['label']. "  ".$count['count']."<br />"; 
    }
}

I've not tested but it's based on the code from given URL. I've just echoed it to verify if the correct data is being displayed. Further you can do what's you required for you actual requirement.

You can use the following code to get the post count published by a user in the author.php file

$author_id = get_queried_object_id();

$getAuthorPosts = new WP_Query(
    array(
        "post_type"      => "post", // Replace "post" with your custom post type. e.g. "book" OR "WHATEVER"
        "author"         => $author_id,
    )
);

echo $getAuthorPosts->found_posts; // Number of posts published by the author.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745080057a4610070.html

相关推荐

  • Showing User&#39;s Post Counts by Custom Post Type in the Author.php?

    i see code in link how to showing User's Post Counts by Custom Post Type in the Admin's User List, but i want

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信