Title: [Plugin: Duplicate Post] Duplicate Attachments
Last modified: August 19, 2016

---

# [Plugin: Duplicate Post] Duplicate Attachments

 *  [devbit](https://wordpress.org/support/users/devbit/)
 * (@devbit)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/plugin-duplicate-post-duplicate-attachments/)
 * If you wish to duplicate all of the attachments of a post/page as well, add the
   following code to your themes **function.php** file. Most won’t have a use for
   this, but I had a site that embeded uploaded images dynamically and needed to
   be duplicated into other areas.
 *     ```
       function dupeAttachments($pID, $oldpost){
       	global $wpdb;
       	//1. get old attachments
       	$attachments = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = $oldpost->ID AND post_type = 'attachment'");
       	//2. duplicate old attachments data
       	foreach($attachments as $att):
       		$wpdb->query("INSERT INTO $wpdb->posts (post_title) VALUES ('')");
       		$newID = $wpdb->insert_id;
       		$IDs[] = array( 'old' => $att->ID, 'new' => $newID );
       		$query = "UPDATE $wpdb->posts SET ";
       		foreach( $att as $key=>$val ){
       			if( $key == 'post_name'):
       				$query .= '<code>'.$key.'</code> = "'.str_replace('"','\"',$val).'-2", ';
       			elseif( $key == 'post_parent' ):
       				$query .= '<code>'.$key.'</code> = "'.$pID.'", ';
       			elseif ($key != 'ID'):
                   	$query .= '<code>'.$key.'</code> = "'.str_replace('"','\"',$val).'", ';
       			endif;
       		}
       		$query = substr($query,0,strlen($query)-2); # lop off the extra trailing comma
       		$query .= " WHERE ID=$newID";
       		if( $wpdb->query($query) ){}else{echo $query; exit;}
       		$query = false;
       	endforeach;
       	// duplicate attachment meta data
       	foreach($IDs as $id):
       		$meta = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = ".$id['old']);
       		foreach( $meta as $mt ){
       			$query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ('".$id['new']."', '".$mt->meta_key."', '".str_replace("'","\'",$mt->meta_value)."')";
       			if( $wpdb->query($query) ){}else{echo $query;exit;}
       			$query = false;
       		}
       	endforeach;
       }
       add_action("dp_duplicate_post", "dupeAttachments", 1, 2);
       add_action("dp_duplicate_page", "dupeAttachments", 1, 2);
       ```
   
 * [http://wordpress.org/extend/plugins/duplicate-post/](http://wordpress.org/extend/plugins/duplicate-post/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Taipa](https://wordpress.org/support/users/taipa/)
 * (@taipa)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/plugin-duplicate-post-duplicate-attachments/#post-1340487)
 * Great idea, didn’t quite work for me.
    Instead of automatically going it, it 
   generated an SQL query that I had to enter manually into the database in order
   to duplicate the images (meaning it was faster to just re-upload images if you
   only had one per page/post). It also did not duplicate any image size settings(
   eg thumbnails, etc).
 * If you just need basic image duplication, this should be fine for you.
 *  [chavo](https://wordpress.org/support/users/chavo/)
 * (@chavo)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/plugin-duplicate-post-duplicate-attachments/#post-1340508)
 * Great function. I need it because of some images galleries I’m publishing. But
   the function is not working for me. When I click “duplicate” the duplication 
   is not processed and I get this message:
 * `UPDATE wp_posts SET post_author = "1", post_date = "2010-09-11 15:02:07", post_date_gmt
   = "2010-09-11 15:02:07", post_content = "", post_title = "yerbatal 10", post_excerpt
   = "", post_status = "inherit", comment_status = "closed", ping_status = "closed",
   post_password = "", post_name = "yerbatal-10-2", to_ping = "", pinged = "", post_modified
   = "2010-09-11 15:02:07", post_modified_gmt = "2010-09-11 15:02:07", post_content_filtered
   = "", post_parent = "796", guid = "http://www.kraus.com.ar/v2/wp-content/uploads/
   yerbatal-10.jpg", menu_order = "1", post_type = "attachment", post_mime_type 
   = "image/jpeg", comment_count = "0" WHERE ID=797`
 * What’s wrong?
 * ps: sorry for my english.
 *  [jordidada](https://wordpress.org/support/users/jordidada/)
 * (@jordidada)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/plugin-duplicate-post-duplicate-attachments/#post-1340546)
 * Thanks for the code!
 * To make it work i have removed the `<code>` tags.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘[Plugin: Duplicate Post] Duplicate Attachments’ is closed to new replies.

 * 3 replies
 * 4 participants
 * Last reply from: [jordidada](https://wordpress.org/support/users/jordidada/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/plugin-duplicate-post-duplicate-attachments/#post-1340546)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
