0

Thanks to everyone who takes a look. I've spent countless hours trying to make this work, but no matter what I tried it still doesn't work. Hopefully I'm missing something simple, but I'm stuck...

Here's my code on the submit page where I add a new page and a cron job to delete it in the future:

$seconds = time() + $_POST["days"] * 86400;
wp_schedule_single_event($seconds, 'crondelete', $new_post_id );

And here's the relevant code in functions.php:

add_action( 'crondelete', 'delete_page_in');
function delete_page_in($args) {
wp_delete_post($args);
}

The cron part is working properly: enter image description here

But it looks like the parameter isn't passed to the function, so once I run the cron job- nothing happens(the page won't delete).

2
  • The third parameter should be an array of arguments - check this example.
    – Sally CJ
    Aug 22, 2019 at 22:29
  • 1
    Thank you Sally, I've checked the answer from below first, but it's essentially the same issue as you pointed out.
    – StefanR
    Aug 23, 2019 at 7:45

1 Answer 1

0

Comparing your code to the Wordpress function wp_schedule_single_event, it appears you failed to indicate that parameters would be passed.

Perhaps the following version of your code will work for you.

add_action( 'crondelete', 'delete_page_in',10,1);
wp_schedule_single_event($seconds, 'crondelete', array($new_post_id ));

Notice the add_action now includes two extra parameters: 10=Priority, and 1=Accepted Arguments to be passed.

2
  • Thank you Mike, it worked :) PS I've already tried those parameters before, but as far as I understand the accepted arguments aren't needed in this case, since it's 1 by default. I think the array part is what I was missing.
    – StefanR
    Aug 23, 2019 at 7:46
  • Glad it worked for you. For others' sake ... WHile the additional parameters (10,1) are not required in this instance, I always include them for readability and consistency with other code blocks. That makes it easier for junior developers to spot patterns in my code blocks. Aug 23, 2019 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.