Skip to content

Instantly share code, notes, and snippets.

@benmay
Created February 17, 2013 04:48
Show Gist options
  • Save benmay/4970230 to your computer and use it in GitHub Desktop.
Save benmay/4970230 to your computer and use it in GitHub Desktop.
Had to split a repeatable section from an ACF post, to it's own post, and then use Posts 2 Posts plugin to preserve the relationship. This managed to keep all the meta data intact for ACF to pick it up (as long as the fields in the new group had the same name) Top level repeater was 'brands' under a post in the advertisers cpt.
$args = array(
'post_status' => array( 'pending', 'draft', 'publish' ),
'posts_per_page' => -1,
'post_type' => 'advertisers'
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
$query->the_post();
the_title();
$num_brands = get_post_meta( $post->ID, 'brands' , true );
$advertiser_id = $post->ID;
$post_meta = get_post_meta( $post->ID );
$count = 0;
while( $num_brands > $count )
{
$new_post = array(
'post_title' => $post_meta[ 'brands_'.$count.'_brand-name' ][0],
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'brands'
);
$pid = wp_insert_post( $new_post );
foreach( $post_meta as $meta_key => $val )
{
if (strpos( $meta_key, 'brands_' . $count ) !== false)
{
$new_key = str_replace( 'brands_' . $count . '_' , '', $meta_key );
add_post_meta( $pid, $new_key, $val[0] );
}
}
global $wpdb;
$wpdb->query("INSERT INTO `wp_p2p` ( `p2p_id` , `p2p_from` , `p2p_to` , `p2p_type` ) VALUES ( NULL , '{$advertiser_id}', '{$pid}', 'brands_to_advertisers' );");
$count++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment