首页 文章

在cron作业中使用rest_do_request()?

提问于
浏览
0

我正在尝试在wordpress中的cron作业中发出一个休息请求,但是如果我在wp_cron触发的函数中有 rest_do_request() 并且它完全执行了's not finishing the job. When executing the cron job forced (with a debug plugin), it',但我在日志 Undefined index: path in "../wp-content/mu-plugins/wpengine-common/plugin.php at line 2030" 中看到了错误 . 网站目前在WPEngine上托管,他们说这些问题并非来自他们,即使PHP通知来自他们的插件 .

由cron触发的函数:

//Sample rest_do_request() that creates a new post
function cron_rings_sync_catalog_bd895153() {

    $request = new WP_REST_Request('POST', '/wp/v2/posts');
    $request->set_param('title', 'Some Title');
    $request->set_param('content', 'Some Content for the new post');
    $response = rest_do_request($request);
}
//Hook to Custom Cron Job
add_action( 'rings_sync_catalog', 'cron_rings_sync_catalog_bd895153', 10, 0 );

目前为了测试这个功能是在主题的 functions.php 文件中添加的,并且请求是为帖子完成的,但我在CPT(环)上用它来路由 '/wp/v2/rings'

我无法弄清楚为什么 rest_do_request() 在cron作业中使用时无效 .

我能够记录cron事件发出的请求: {"data":{"code":"rest_cannot_create","message":"Sorry, you are not allowed to create posts as this user.","data":{"status":401}},"headers":[],"status":401}

1 回答

  • 0

    如果有人遇到这个问题,我想出了这个问题 . 问题是WP_Cron没有授权发出POST请求 . 作为一个快速修复,我在 $request = new WP_REST_Request('POST', '/wp/v2/posts'); 之前添加了 wp_set_current_user($admin_id); 在我的函数中 .

相关问题