WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to display different things to new users that have not created a post versus users who have created a post.

I tried this, but it did not work for custom post types (only normal posts) and it does not account for drafts or pending posts:

<?php if (count_user_posts(1)>=1) { blah blah blah } else {Welcome, blah blah blah }; ?>
share|improve this question
up vote 6 down vote accepted

Hi @Carson:

A simple function to address what you are asking for might be the function yoursite_user_has_posts() which leverages the built in WP_Query class:

function yoursite_user_has_posts($user_id) {
  $result = new WP_Query(array(
  return (count($result->posts)!=0);

You can then call it from your theme like this:

$user = wp_get_current_user();
if ($user->ID)
  if (yoursite_user_has_posts($user->ID))
    echo 'Thank you for writing!';
    echo 'Get Writing!';
share|improve this answer
that won't work as count($result->posts) will return 10 or something like that. you need to either replace that with $result->found_posts or add the posts_per_page => -1 argument. not to mention the huge resource drag. if the user has 1000+ posts you'll probably get your account suspended for this on a shared host :) – onetrickpony Dec 22 '10 at 9:01
@Ambitious Amoeba - Not sure why you say it won't work. The OP wanted to know if zero or if not zero and 10 is not zero. Fair point about the resource, except it's using the default which means only 10 posts anyway, not 1000+. Yes, there are 20 different ways to do this, but it achieves the OP's goal. There are definitely other more performant ways to achieve it but they require more code and I was trying to keep it simple because, based on the OP's question it seems that simpler would be better for him. – MikeSchinkel Dec 22 '10 at 11:00
Remember the user only wants to know if a user has created 1 or more posts, we don't need to select all of them to figure out if the user has at least 1 post.. (something i think you overlooked when commenting).. though @Mike i'd sugget updating the code sample to check a particular post type, else that code will match all sorts(when the user did specificy a particular type). – t31os Dec 22 '10 at 11:15
.. further... you don't even need to return a count, if the query is empty then we know the query got 0 results(ie. the user has no posts), so you could do a return value based on whether the result was empty(true/false). If the query was not empty we can therefore postulate that there are posts associated with the user(Mike you following what i'm getting at?).. – t31os Dec 22 '10 at 11:18
@t31os - Yes, I hear ya. I guess I was getting lazy on answer 193... :) Is that any better? Yes I could do direct SQL and make it tighter but I prefer not to use direct SQL in WordPress if possible. – MikeSchinkel Dec 22 '10 at 11:22

see Best Collection of Code for your functions.php file:

function atom_count($user_id, $what_to_count = 'post') {
  global $wpdb;    
  $where = $what_to_count == 'comment' ? "WHERE comment_approved = 1 AND user_id = {$user_id}" : get_posts_by_author_sql($what_to_count, TRUE, $user_id);
  $from = "FROM ".(($what_to_count == 'comment') ? $wpdb->comments : $wpdb->posts);    
  $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) {$from} {$where}"));
  return $count;

if you want this to count all posts, regardless of the type, and status, change get_posts_by_author_sql() with "WHERE post_author = '{$user_id}'"

didn't test that, but it should work...

share|improve this answer
This will break with the next DB change. Use the API whenever possible, not some custom SQL. – toscho Dec 22 '10 at 11:43
I'll add "Use the API whenever possible and there's isn't a more efficient method". DB doesn't change every day, I doubt that any of the fields in the function above ever will. – onetrickpony Dec 22 '10 at 13:09
Amoeba - Efficiency can be overrated. I'd rather spend a few milliseconds than to learn that code I wrote last year broke and the client wants it fixed now! :) – MikeSchinkel Dec 24 '10 at 0:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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