Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

php - Alternative to wp_update_post() in Wordpress REST API

I'm a beginner and I'm trying to write a Wordpress Plugin in PHP to auto-update content on multiple pages. I managed to make it work, it updates correctly, however it breaks multiple page options (controlled by the theme). I believe it's caused by the wp_update_post() function.

I'm looking for the REST API equivalent of this, since it works successfully when I tried with Python.

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

Thank you !

question from:https://stackoverflow.com/questions/65660439/alternative-to-wp-update-post-in-wordpress-rest-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found the answer :

                 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);

In case someone needs it ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...