Automatically delete inactive users after 2 months

Your query is wrong because your third argument to TIMESTAMPDIFF is incorrect.

You should be using meta_value instead of SELECT meta_value.

SELECT user_id FROM wp_usermeta WHERE meta_key = 'wp_wp_kc_last_active_time' AND TIMESTAMPDIFF( second, now(), TIMESTAMP(meta_value) ) > 5184000;

Try that and see if the results start looking correct.

I just checked over the mySQL Date Function Docs and you appear to be doing this wrong.

Try the following instead:

SELECT user_id FROM wp_usermeta WHERE meta_key = 'wp_wp_kc_last_active_time' AND TIMESTAMPDIFF( MONTH, NOW(), FROM_UNIXTIME(meta_value) ) > 2;

Or Maybe…

global $wpdb;

$query = <<<SQL
 SELECT user_id 
 FROM {$wpdb->usermeta} 
 WHERE 
    meta_key = 'wp_wp_kc_last_active_time'
    AND DATEDIFF( NOW(), FROM_UNIXTIME( meta_value ) ) > 60
SQL;

$query = $wpdb->prepare( $query );

Which should gather the right users. If not, try it without the datediff statement and see if anything is returned. If not, then something is amiss with the meta_key (e.g. is it really wp_wp_… or just wp_kc_…)

Leave a Comment