Ok, so I have a plugin called premium add-ons, it has a feature to duplicate a post exactly how I want, only problem is I can’t do it from the API so I’m trying to add that functionality.
I think if I can pass the post correctly, this plugin will take care of it.
Heres the API route I created, in my functions.php.
add_action( 'rest_api_init', function () {
register_rest_route( 'duplicatePost/v1', 'posts/(?P<id>\d+)', array(
'methods' => 'POST',
'callback' => 'duplicate_post',
) );
} );
Here is the function I’m trying to pass the request to:
public function duplicate_post(WP_REST_Request $request) {
$post = get_post($request['post_id']);
$post = sanitize_post( $post, 'db' );
$duplicated_post_id = self::insert_post( $post );
if ( ! is_wp_error( $duplicated_post_id ) ) {
self::duplicate_post_taxonomies( $post, $duplicated_post_id );
self::duplicate_post_meta_data( $post, $duplicated_post_id );
}
die();
}