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'm trying to make a list of users who have published posts within two given dates. My attempts using WP_User_query just work for one blog ID and takes time to load because it runs through a lot of foreach and if statements. Also it seems that not all users are stored in the arrays I'm using. I have a feeling that I'm doing it totally wrong but I can't find any wordpress function which could help me.

I would like to do this with an wpdb query but I don`t know how to check for all users and their post count.

My current code is this:

$blogids = array( 2, 4, 5, 6 );

global $blog_id;

$startdate = "2012-03-01";
$enddate = "2012-04-31";

//$wp_user_search = new WP_User_Query( array( 'role' => 'subscriber', 'blog_id' => $blog_id, 'fields' => array('ID', 'user_nicename', 'display_name') ) );

$userlist = array();
$usercountlist = array();

foreach($blogids as $blogid) {

    $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber', 'blog_id' => $blogid, 'fields' => array('ID', 'user_nicename', 'display_name') ) );
    $usernicenames = $wp_user_search->get_results();

foreach($usernicenames as $usernicename){

    $user_count = $wpdb->get_var( $wpdb->prepare( "
    SELECT COUNT(ID) FROM ". $wpdb->base_prefix . $blogid ."_posts WHERE post_status = 'publish' AND post_type = 'post'
    AND post_author = ".$usernicename->ID." AND post_date >= '".$startdate."'
    AND post_date < '".$enddate."' HAVING COUNT(ID) > 0
    " ) );

    //$userlist[$user_count] = $usernicename->user_nicename;

    if($user_count) :
      $userlist[] = $usernicename->display_name;
      $usercountlist[] = $user_count;



$userlist = array_combine($userlist, $usercountlist);

$str = serialize($userlist); // Serialises the array $array and store as $str
$strenc = urlencode($str);  // Encode the serialised array

$temp_folder = str_replace("//", "/", $_SERVER['DOCUMENT_ROOT']."/_temp/");

$file = $temp_folder.'userstats.txt';
file_put_contents($file, $strenc);

$content2 = file_get_contents($file);
$arr = unserialize(urldecode($content2));

echo "<ol>";
$i = 0;
foreach ( $arr as $key => $value ) {
if($value > 0 && $i < 10):
echo "<li>".$key." (".$value.")</li>";
endif ;
echo "</ol>"; 
share|improve this question

Your Answer


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

Browse other questions tagged or ask your own question.