Skip to content

Instantly share code, notes, and snippets.

@fritids
Forked from bordoni/get_last_month_users.php
Created February 10, 2014 11:55
Show Gist options
  • Save fritids/8914570 to your computer and use it in GitHub Desktop.
Save fritids/8914570 to your computer and use it in GitHub Desktop.
<?php
$role = 'subscriber';
if ( false === ( $userids = get_transient( "monthly_userids.{$role}" ) ) ) { // If the transient isn't set do it!
global $wpdb;
$start_date = date('Y-m-d H:i:s', strtotime('first day of this month'));
$end_date = date('Y-m-d H:i:s', strtotime('first day of next month'));
$userids = $wpdb->get_col( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS `user`.`ID` as `ID` FROM `{$wpdb->users}` AS `user`, `{$wpdb->usermeta}` AS `meta` WHERE 1=1 AND (`meta`.`user_id` = `user`.`ID` AND `meta`.`meta_key` = 'em_capabilities' AND `meta`.`meta_value` LIKE '%s') AND `user`.`user_registered` BETWEEN '%s' AND '%s' GROUP BY `user`.`ID`;", "%{$role}%", $start_date, $end_date ) );
set_transient( "monthly_userids.{$role}", $userids, 60*60*12 );
}
// Ready to use "$userids" as an array of the available users.
// To expire the data from the "object" cache, use the code below
$role = 'subscriber';
delete_transient("monthly_userids.{$role}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment