Regarding ‘wp_insert_post’ function, you may want to insert posts with assigned IDs for some reason. Yes, you can do it by using $post->import_id. There is also $post->ID parameter to determine ID. I’ll describe difference between $post->import_id and $post->ID of wp_insert_post.
- If you want to insert a new post with arbitrary ID, use $post->import_id.
- If you want to update a existing post, use $post->ID. Without existing post with the ID, the function will do nothing.
When you use $post->import_id, please make sure that there is no existing post with ID same as import_id. If the post exists, wp_insert_post will make a new post with automatically generated ID which is not what you want.
Creating a post with assigned ID is useful when you import posts from another site because you can convert internal links easily before import them. Bellow is a patch to modify ‘Movable Type and TypePad Importer’ (Ver. 0.4) to import posts with pre-assigned number.
--- movabletype-importer.php.orig 2011-01-15 09:53:03.000000000 +0900 +++ movabletype-importer.php 2011-08-02 20:22:59.000000000 +0900 @@ -388,6 +388,10 @@ $slug = trim( substr($line, strlen('BASENAME:')) ); if ( !empty( $slug ) ) $post->post_name = $slug; + } else if ( 0 === strpos($line, 'ID:') ) { + $post_id = trim( substr($line, strlen('ID:')) ); + if ( !empty( $post_id ) ) + $post->import_id = $post_id; } else if ( 0 === strpos($line, 'STATUS:') ) { $status = trim( strtolower( substr($line, strlen('STATUS:')) ) ); if ( empty($status) )
With this patch, you can import posts with assigned IDs by using ‘ID’ lines in your MT data.
AUTHOR: blogger323 ID: 1234 TITLE: Hello World!