Skip to content
Advertisement

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 !

Advertisement

Answer

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 😉

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement