• Resolved S. Larrabee

    (@fatlabllc)


    I have been struggling with this for a little while now and hoping someone can provide some advice.

    I am writing a plugin that includes a wp-cron job to delete records from a table after they are 24 hours old. The cron job is created just fine on activation and using several cron related plugins I can see and execute the cron job without issue. However the table rows meeting the 24 hour criteria are not deleted. My code is below.

    Thanks so much for any insight!

    register_activation_hook( __FILE__, 'sap_create_schedule' );
    
    	function sap_create_schedule() {
    		wp_schedule_event( time(), 'hourly', 'sap_hourly_cleanup' );
    	}
    
    	add_action( 'sap_hourly_cleanup', 'sap_cleanup' );
    
    	function sap_cleanup() {
    	$table_name = $wpdb->prefix . "sap";
    	$wpdb->query("DELETE FROM $table_name WHERE creation_time >= now() + INTERVAL 1 DAY");
    	}
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Your query is saying delete rows where creation time is later than one day from now. You want earlier, or <=. You are thinking as though you are dealing with time differences or age, you are dealing with absolute time that marks a spot on the time line.

    Thread Starter S. Larrabee

    (@fatlabllc)

    Thank you so much for taking the time to look through that, I appreciate the second set of eyes! That was it. I must have looked at that for a couple hours.

    Moderator bcworkz

    (@bcworkz)

    You’re most welcome! I’ve been through similar confusion many times, it happens to the best of us.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Getting delete query to work in wp-cron’ is closed to new replies.