Check if a user has authored any posts

/** 
 * @since version 1.0
 *
 * Check if a user has authored any posts
 *
 * Pass the author user name into the function to check if posts exist under a certain authorname.
 *  
 * Use:
 * call function and pass in author username -> <?php author_has_posts('eallen'); ?>
 *
 * @returns true if posts exist
 * @returns false otherwise
 */
function author_has_posts( $author ) {
  $args = array(
    'author_name' => $author
  );
  $query = new WP_Query($args);
  if($query->have_posts()) {
    return true;
  }
  //wp_reset_postdata();
  return false;
}

A simple way to check if a user has created any posts. Useful if you need to add extra style or functionality to authors. Place this wpsnipp.it in your child theme’s functions.php file. Can be used outside the loop.

Leave a Reply