Skip to content

Instantly share code, notes, and snippets.

@stokesman
Last active December 7, 2021 22:33
Show Gist options
  • Save stokesman/0de15c080b2390e9a802e9e7955f9f39 to your computer and use it in GitHub Desktop.
Save stokesman/0de15c080b2390e9a802e9e7955f9f39 to your computer and use it in GitHub Desktop.
WP insert new posts from remote WP
<?php
['body' => $payload] = wp_remote_get( '/wp-json/wp/v2/posts/?categories=10&per_page=20' );
$post_list = json_decode( $payload );
foreach ( $post_list as $post_data ) {
$post = [
'post_category' => [ 7 ],
'post_title' => $post_data->title->rendered,
'post_status' => 'publish',
'post_date' => $post_data->date,
'post_content' => $post_data->content->rendered,
'post_excerpt' => $post_data->excerpt->rendered,
];
wp_insert_post( $post, false );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment