Skip to content

Instantly share code, notes, and snippets.

@thisislawatts
Created September 9, 2012 13:00
Show Gist options
  • Save thisislawatts/3684169 to your computer and use it in GitHub Desktop.
Save thisislawatts/3684169 to your computer and use it in GitHub Desktop.
A brute force method for migrating content out of Wordpress Galleries into a ACF repeat Gallery
<?php
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
/**
* Move all ACF media from a post into a ACF flexible content box.
* NB: Custom WPDB prefix, and names of custom fields
*/
global $wpdb;
// Get all our post ID's
$posts = $wpdb->get_results("SELECT `id` FROM `vwbs_posts` WHERE `post_type` = 'projects' OR `post_type` = 'ideas'", OBJECT);
foreach($posts as $post) {
if ($post->id != 96) {
$iterator = 0;
while (has_sub_field('media', $post->id)) :
update_post_meta($post->id, "media_{$iterator}_module",'a:1:{i:0;s:5:"image";}');
update_post_meta($post->id, "media_{$iterator}_module_0_image", get_sub_field('image', $post->id));
update_post_meta($post->id, "media_{$iterator}_module_0_image_caption","");
update_post_meta($post->id, "media_{$iterator}_module_0_type_colour",'black');
update_post_meta($post->id, "media", $iterator);
$iterator++;
endwhile;
echo "<hr/>";
}
}
die();
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
/**
* Move all image attachments from a post into a ACF custom gallery.
* NB: Custom WPDB prefix, and names of custom fields
*/
global $wpdb;
// Get all our post ID's
$posts = $wpdb->get_results("SELECT `id` FROM `vwbs_posts` WHERE `post_type` = 'projects' OR `post_type` = 'ideas'", OBJECT);
foreach($posts as $post) {
// Get all images from this post
$images = $wpdb->get_results("SELECT `ID`,`post_excerpt` FROM `vwbs_posts` WHERE `post_parent` = $post->id", OBJECT);
$thumbnail_id = get_post_thumbnail_id($post->id);
$iterator = 0;
foreach($images as $image) {
if ($thumbnail_id != $image->ID) {
update_post_meta($post->id, "media_{$iterator}_image", $image->ID);
update_post_meta($post->id, "media_{$iterator}_caption",$image->post_excerpt);
$iterator++;
}
}
update_post_meta($post->id, "media", $iterator);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment