This post has been edited on 30 Nov 2015 and I have answered it myself. The correct script is at the bottom of this post. Thanks and good luck!
The problem:
To explain, I am working on localhost using MAMP, I am using WP ALL IMPORT plugin to get data into wordpress..I have a theme called BOOK YOUR TRAVEL. It has a custom posts and one particular custom field called "accommodation_images". It stores image ids in whole array which is serialized. For long I did not realize that I did not have to serialize it my self. I found scripts for similar purpose and have tried working with them.
I managed to get all images imported and attached with the plugin, and then importing to custom field "accommodation_images" was harder. as soon as it hits foreach I get empty array. when I comment foreach loop I get one entry only if I use following line
The single entry was obviously last one as it loops trough all..
but it is in a right format:
This is how the entry should look like in the db when more then 1
unserialize shows this array as
Below is the code that I have been working around
I needed to fix the loop in order to get the rest of the images added to array.
==================================================
The problem:
To explain, I am working on localhost using MAMP, I am using WP ALL IMPORT plugin to get data into wordpress..I have a theme called BOOK YOUR TRAVEL. It has a custom posts and one particular custom field called "accommodation_images". It stores image ids in whole array which is serialized. For long I did not realize that I did not have to serialize it my self. I found scripts for similar purpose and have tried working with them.
I managed to get all images imported and attached with the plugin, and then importing to custom field "accommodation_images" was harder. as soon as it hits foreach I get empty array. when I comment foreach loop I get one entry only if I use following line
Code:
$atts[] = array("image" =>$attid);
The single entry was obviously last one as it loops trough all..
but it is in a right format:
Code:
a:1:{i:0;a:1:{s:5:"image";s:5:"55079";}}
This is how the entry should look like in the db when more then 1
Code:
a:3:{i:0;a:1:{s:5:"image";s:5:"16487";}i:1;a:1:{s:5:"image";s:5:"77726";}i:2;a:1:{s:5:"image";s:5:"77722";}}
unserialize shows this array as
Code:
Array (
[0] => Array
(
[image] => 16487
)
[1] => Array
(
[image] => 77726
)
[2] => Array
(
[image] => 77722
)
)
Below is the code that I have been working around
Code:
//This hook is called after WP All Import creates/updates a post meta.
//$pid – the ID of the post/page/Custom Post Type that was just created.
//$attid – the ID of the attachment
//$image_filepath – the full path to the file: C:\path\to\wordpress\wp-content\uploads\2010\05\filename.png
add_action('pmxi_gallery_image', 'update_images_meta', 10, 3);
function update_images_meta( $pid, $attid, $image_filepath ) {
//$attachment = get_post($attid);
// do something with $attachment image
// Get all the image attachments for the post
$param = array(
'post_parent' => $pid,
'post_type' => 'attachment',
'post_mime_type' => 'image'
);
$attachments = get_posts($param);
// Initialize the array
$atts = array();
// Fill the array with attachment ID's
foreach ($attachments as $attachment) {
//$atts[] = $attachment->ID;
array_push($atts, array("image" =>$attid));
//$atts[] = array("image" =>$attid);
}
//$serialized = serialize($atts);
//echo $serialized;
// unhook this function so it doesn't loop infinitely
//remove_action( 'save_post', 'update_images_meta', 10 );
// Update the post's meta field with the attachment arrays
update_post_meta($pid, 'accommodation_images', $atts);
// renable save post action
//add_action( 'save_post', 'update_images_meta', 10 );
}
?>
I needed to fix the loop in order to get the rest of the images added to array.
==================================================