In this tutorial, we’ll walk you through the process of importing posts from one WordPress site to another.
Requirements
- Code Snippets Plugin
- Free WPGetAPI plugin
- API to Posts plugin
- A WordPress site to get the posts
Step 1: Setup API & endpoint
Start by setting up the API to retrieve posts from the another WordPress site.
This is the WordPress site API settings required:
In this example:
- The another wordpress site is: https://ilj.local
- The endpoint used is: /wp-json/wp/v2/posts — this is the standard endpoint for retrieving posts via the WordPress REST API.
For detailed reference, check the WordPress REST API documentation.
Step 2: Add Custom Code for Pagination (Optional)
By default, the WordPress REST API returns 10 posts per request. To fetch all posts with pagination, you can use the following custom snippet. Add it to your theme’s functions.php file, or utilize the Code Snippets Plugin.
/**
* Modify the raw API data returned by WPGetAPI.
*
* Adds pagination support by extracting `x-wp-totalpages` or `X-WP-TotalPages` from the headers
* and parsing `page` from query variables if available.
*
* @param mixed $data The raw API response data.
* @param object $wpgetapi The WPGetAPI object instance.
*
* @return mixed Modified response data or original data.
*/
function wpgetapi_modify_output( $data, $wpgetapi ) {
if ( empty( $data ) ) {
return __( 'Sorry, no data returned.', 'text-domain' );
}
if ( 'ilj_local' == $wpgetapi->api_id ) {
$response = array();
$response_header = $wpgetapi->response_header;
$query_param = $wpgetapi->args;
$response['posts'] = $data;
$total_pages_header = isset( $response_header['x-wp-totalpages'] )
? $response_header['x-wp-totalpages']
: ( $response_header['X-WP-TotalPages'] ?? null );
if ( ! empty( $total_pages_header ) ) {
$response['total_pages'] = absint( $total_pages_header );
$response['page'] = 1;
if ( ! empty( $query_param['query_variables'] ) ) {
$vars = explode( ',', $query_param['query_variables'] );
$final_array = array();
foreach ( $vars as $var ) {
$couple = explode( '=', $var );
if ( count( $couple ) === 2 ) {
$final_array[ trim( $couple[0] ) ] = trim( $couple[1] );
}
}
$response['page'] = isset( $final_array['page'] ) ? absint( $final_array['page'] ) : 1;
}
return $response;
}
}
return $data;
}
add_filter( 'wpgetapi_raw_data', 'wpgetapi_modify_output', 10, 2 );
Replace ilj_local
with the actual API ID you’ve configured in WPGetAPI.
Step 3: Create Posts from the another WordPress site
Once your API is set up and optional pagination is handled, follow these steps to import the posts.
- configure how the API data will be imported
- select ‘Post’ as the post type
- run the ‘API Importer’
- map the API data to the Post fields
- run the ‘Post Creator’
That’s it!
You’ve successfully imported posts from another WordPress site. You can map data to standard post fields such as title, content, excerpt, date, categories, featured images, custom fields and more.
Going further
Check out the full tutorial with a step-by-step guide here. This article runs through the exact process of creating posts from posts from one WordPress site to another WordPress site.