Display text if current user has written 1 or more posts in a custom post type

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(
    'author'=>$user_id,
    'post_type'=>'any',
    'post_status'=>'publish',
    'posts_per_page'=>1,
  ));
  return (count($result->posts)!=0);
}

You can then call it from your theme like this:

<?php
$user = wp_get_current_user();
if ($user->ID)
  if (yoursite_user_has_posts($user->ID))
    echo 'Thank you for writing!';
  else
    echo 'Get Writing!';
?>

Leave a Comment