Wordpress REST API中wp_update_post()的替代方案



我是一个初学者,我正在尝试用PHP编写一个Wordpress插件来自动更新多个页面上的内容。我设法使它工作,它正确地更新,但它打破了多个页面选项(由主题控制(。我认为这是由wp_update_post()函数引起的。

我正在寻找与此类似的REST API,因为当我尝试使用Python时,它可以成功地工作。

<?php
.
..
... 
$new_content = str_replace($old_data, $new_data, $content);
$edited_post = ['ID'=> $pageID, 'post_content' => $new_content];
wp_update_post( $edited_post);

谢谢!

找到答案:

wp_set_current_user(1); // IMPORTANT FOR CRONJOB
$request = new WP_REST_Request( 'PUT', '/wp/v2/pages/'.$pageID );
$request->set_query_params( [ 'content' => $new_content ] );
$rest_response = rest_do_request($request);
wp_set_current_user(0);

以防有人需要;(

最新更新