最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Get count of custom post type created by current user

programmeradmin3浏览0评论

I'm trying to get the count of published posts in a custom post type for the current user (to display on a profile page).

I found this here on the forums:

<?php
global $wp_query;
$curauth = $wp_query->get_queried_object();
$post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'user_video' AND post_status = 'publish'");
?>

But, it's just giving me a big fat ZERO even though there are definitely published posts of that type. What am I doing wrong and is there a better way to do this?

I'm trying to get the count of published posts in a custom post type for the current user (to display on a profile page).

I found this here on the forums:

<?php
global $wp_query;
$curauth = $wp_query->get_queried_object();
$post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'user_video' AND post_status = 'publish'");
?>

But, it's just giving me a big fat ZERO even though there are definitely published posts of that type. What am I doing wrong and is there a better way to do this?

Share Improve this question asked Apr 12, 2013 at 20:22 EcksteinEckstein 1,1194 gold badges27 silver badges52 bronze badges 2
  • Did you try COUNT(ID)? – Andrew Bartel Commented Apr 12, 2013 at 20:29
  • I had not, but I just tried it and it didn't work still. – Eckstein Commented Apr 12, 2013 at 20:36
Add a comment  | 

6 Answers 6

Reset to default 5

I'm posting a new answer because I found this thread while searching for the same thing and the solutions here weren't optimal.

The post_type argument can be a string or an array of post types.

function custom_count_posts_by_author($user_id, $post_type = array('post', 'page', 'custom_post_type'))
{
    $args = array(
        'post_type'      => $post_type,
        'author'         => $user_id,
        'post_status'    => 'publish',
        'posts_per_page' => -1
    );

    $query = new WP_Query($args);

    return $query->found_posts;
}

I would suggest using get_posts() instead of query_posts() for your purpose.

To create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in a sidebar widget), try making a new instance of WP_Query or use get_posts(). [source]

It also looks simpler now :)

echo count( get_posts( array( 
    'post_type' => 'user_video', 
    'author'    => get_current_user_id(), 
    'nopaging'  => true, // display all posts
) ) );

OK, after more Googleing, this seems to work without having to use MySQL and dive into the database directly:

<?php 
        $authorid = get_current_user_id();
        query_posts(array( 
            'post_type' => 'user_video',
            'author' => $authorid,
        ) ); 
            $count = 0;
            while (have_posts()) : the_post(); 
                $count++; 
            endwhile;
            echo $count;
        wp_reset_query();
    ?>

https://codex.wordpress/Function_Reference/count_user_posts

<?php $user_post_count = count_user_posts( $userid , $post_type ); ?>

In authors profile for me works great this (ait-dir-item is custom post type name)

<?php  
$idecko = get_the_author_meta( 'ID' );
echo count_user_posts( $idecko, 'ait-dir-item' ); ?>     

You need to declare $wpdb as global as well, and use its prefix method.

<?php
global $wp_query, $wpdb;
$curauth = $wp_query->get_queried_object();
$post_count = $wpdb->get_var("SELECT COUNT(ID) FROM ".$wpdb->prefix."posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'user_video' AND post_status = 'publish'");
?>
发布评论

评论列表(0)

  1. 暂无评论