在 Wordpress REST API 中替代 wp_update_post()

Alternative to wp_update_post() in Wordpress REST API

我是初学者,我正在尝试在 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);

万一有人需要它;)