Compare time value in WP_User_Query for sending emails

I am trying to setup a custom plugin that will compare the time (last activity of a user) and send emails to them. I am using the below WP_User_Query and a cron job

function reminder_email() {

// Get the contributors and authors who haven't logged in for 60 days - DO I NEED TO SET UP W3C Total Cache IN SOMEWAY IN ORDER FOR THE Query TO WORK? I DONT HAVE DB OR OBJECT CACHE ENABLED
$remind_users = new WP_User_Query( [   // or new \WP_User_Query syntax?? not sure 
'role'         = [ 'contributor', 'author', ],
'meta_key'     = 'last_activity',
'meta_value'   = strtotime( '-60 days' ),   // IS THIS CORRECT? HOW TO COMPARE THE last_activity 'Y-m-d H:i:s' WITH THE LAST 60 DAYS
'meta_compare' = '',
  ] );

if ( ! empty( $remind_users ) ) { 
foreach( $remind_users-get_results() as $user ) {
    
$message = sprintf( 'Hello %1$s,' , $user-first_name ) . brbr; 
$message .= sprintf( 'Have you forgot us? Please visit our site again.' ) .br; 

wp_mail( $user-user_email , 'Reminder Email for You', $message );
     }
    }
 }
add_action( 'reminder_email', 'reminder_email' );

The last_activity meta_key stores the time in 'Y-m-d H:i:s'.

  1. Can i use the strtotime( '-60 days' ) for getting all the users that have not been active for 60 days?
  2. What is the difference between new WP_User_Query and new \WP_User_Query syntax?
  3. Inside the same function can i use another similar WP_User_Query to send different emails to other type of roles? So to avoid creating 2 cron jobs for this.

I have tried to do :

$today = date('Y-m-d h:i:s');
$remind_users = new WP_User_Query( [  
'role__in' = array( 'contributor', 'administrator' ),
'meta_key'     = 'last_activity',
'meta_query' = array( 
        array(
            'key' = 'last_activity',
            'value' = date('Y-m-d h:i:s', strtotime($today . '-60 days')),
            'compare' = '',
            'type' = 'DATE' // Let WordPress know we're working with date
            )
        )
 ] );

But doesn't give me the users with last activity before 60 days ago. What am i doing wrong here :(

Any help would be appriciated!

Topic wp-user-query wp-cron comparison date-time Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.