2

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?

2
  • Did you try COUNT(ID)? Apr 12, 2013 at 20:29
  • I had not, but I just tried it and it didn't work still.
    – Eckstein
    Apr 12, 2013 at 20:36

6 Answers 6

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;
}
5

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
) ) );
2

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();
    ?>
4
  • Thanks @MichalMau for the valuable info! This was really helpful.
    – Eckstein
    Apr 12, 2013 at 22:27
  • This function is very inefficient because it loops through the results to update the count, while you could just get the posts count using WP_Query.
    – gyo
    Nov 25, 2014 at 11:27
  • Very true. I wrote this answer long ago, before I really understood wp_query.
    – Eckstein
    Nov 25, 2014 at 19:00
  • 1
    I posted a new answer for people landing here :)
    – gyo
    Nov 25, 2014 at 20:16
2

https://codex.wordpress.org/Function_Reference/count_user_posts

<?php $user_post_count = count_user_posts( $userid , $post_type ); ?>
1
  • Can you please add an explanation why this is helpful - thanks. Aug 13, 2015 at 7:11
2

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' ); ?>     
1

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'");
?>
4
  • Hmmm, still not doing it. Is there a better method than this? I literally copied and pasted the first snippet from another answer, so I'm open to alternative ideas if you have them.
    – Eckstein
    Apr 12, 2013 at 21:08
  • Put the whole query in a variable, die that out and take it to mysql workbench at this point then. I'd start with just Select * FROM wp_posts WHERE post_type = 'user_video', finding an author and then working backwards from there. Can you post what the query looks like here too after you echo it? Apr 12, 2013 at 21:14
  • 2
    @AndrewBartel It's easier to parse visually using this formatting. You only need prefix for non-standard tables and no need to concatenate the string if using double-quotes.
    – brasofilo
    Apr 12, 2013 at 21:40
  • Ah, thanks @brasofilo. Learn something new every day. Apr 12, 2013 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.