From f3ea00820e363a063e8ac6ff42446977766981ea Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Sun, 26 Oct 2008 17:05:56 +0000 Subject: [PATCH] Preserve attachment IDs during import. Props mtdewvirus. fixes #7972 git-svn-id: https://develop.svn.wordpress.org/trunk@9357 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/post.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index efe7331c6d..6bd4eea69d 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -2245,7 +2245,7 @@ function wp_insert_attachment($object, $file = false, $parent = 0) { $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_ID, 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', - 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => ''); + 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0); $object = wp_parse_args($object, $defaults); if ( !empty($parent) ) @@ -2341,6 +2341,14 @@ function wp_insert_attachment($object, $file = false, $parent = 0) { if ( $update ) { $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) ); } else { + // If there is a suggested ID, use it if not already present + if ( !empty($import_id) ) { + $import_id = (int) $import_id; + if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) { + $data['ID'] = $import_id; + } + } + $wpdb->insert( $wpdb->posts, $data ); $post_ID = (int) $wpdb->insert_id; }