Let us now learn how to copy posts from one site to another site. Basically, we will be using REST API to GET JSON post data and convert it into a PHP object. In the previous section, we have already created a function for this and we will reuse the code. It is as simple as passing the following request:
$url = 'http://example.com/wp-json/wp/v2/posts/1'; $post = get_json( $url );
Now, to create a copy of the post
, we just have to turn $post
into an array and then pass it to wp_insert_post()
.
To convert $post
, we will follow a standard web development practice. Before the code, let us first spend some time understanding what we need to do.
As already stated, we have to ensure that the data being passed to the function is an array. If the data is an object, we will need to typecast it as an array because the rest of the function will require an array.
Thereafter, once we have an array, we will setup a second array because we will...