WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Duplicate Post] Duplicate Attachments (4 posts)

  1. devbit
    Member
    Posted 4 years ago #

    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/

  2. Taipa
    Member
    Posted 4 years ago #

    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.

  3. chavo
    Member
    Posted 3 years ago #

    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.

  4. jordidada
    Member
    Posted 3 years ago #

    Thanks for the code!

    To make it work i have removed the <code> tags.

Topic Closed

This topic has been closed to new replies.

About this Topic