最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Scheduled post delete - can't pass the cron arguments

programmeradmin0浏览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:

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).

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:

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).

Share Improve this question asked Aug 22, 2019 at 19:45 StefanRStefanR 133 bronze badges 2
  • The third parameter should be an array of arguments - check this example. – Sally CJ Commented 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 Commented Aug 23, 2019 at 7:45
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论