【问题标题】:How to insert post with cron job in Wordpress如何在 Wordpress 中插入带有 cron 作业的帖子
【发布时间】: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


【解决方案1】:

不要忘记 WordPress cron“机制”仍然是“非自动”模拟器。如果访问者(人们、搜索引擎等)每分钟都在访问您的网站 - cron 将起作用。如果访问者不“触摸”您的网站 - cron 将无法工作。

【讨论】:

  • 谢谢,我不知道。
猜你喜欢
  • 2018-04-13
  • 2019-02-22
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 1970-01-01
  • 2012-06-20
  • 2017-12-04
  • 1970-01-01
相关资源
最近更新 更多