Count user posts by type and date

The elegant way to get posts out of the database is to use wp_query. It has all the filters you need:

function wpse70323_count_user_posts_by_type( $userid, $post_type, $year, $month, $day ) {
  $query = new WP_Query( array(
    'author'         => $userid,
    'post_type'      => $post_type,
    'date_query'     => array(
       array(
         'year'      => $year,
         'month'     => $month,
         'day'       => $day,
         ),
        ),
    'posts_per_page' => -1,       
    )); 
  $count = $query->post_count;
  return $count;
  }

It’s also possible to return ranges of dates, depending on your needs. Read the documentation on wp_query for that.

Leave a Comment