【发布时间】:2014-11-30 23:47:28
【问题描述】:
我想制作一个插件,使用 cron 作业每 30 分钟创建一个新帖子。 我已经为我的 cron 作业创建了一个新的时间间隔。 我认为问题在于插入新帖子。我不确定你能帮我吗?
<?php
/*
Plugin Name:
Description:
Version: 1.0
Author:
*/
add_filter('cron_schedules', 'new_interval');
// add once 30 minute interval to wp schedules
function new_interval($interval) {
$interval['minutes_30'] = array('interval' => 30*60, 'display' => 'Once 30 minutes');
return $interval;
}
function InitiateMyCron() {
if (!wp_next_scheduled('MyCronEvent')) {
wp_schedule_event(time(), 'minutes_30', 'MyCronAction');
}
}
function MyCronAction() {
//do my cron every 30 minutes
$new_post = array(
'post_title' => 'Cron job New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => 1,
'post_type' => 'post',
'post_category' => array(6, 2)
);
$post_id = wp_insert_post($new_post);
}
?>
我做错了什么?
提前谢谢你。
【问题讨论】:
-
你检查
wp_insert_post()的返回值了吗?失败时返回 0。 -
我无法调试它,因为它在 cron 作业中,我不知道它是否运行。
标签: php wordpress plugins cron