I have post types Boats and Drills in a one<---> many relationship. I'm working on an efficient way to bulk duplicate Drills to a different boat. I'm currently using CSV exports/imports to achieve this.
A custom field on each drill import-belongs-to-id stores the id of the parent boat I want the drill to attach to.
I'm following the content here to achieve this: https://toolset.com/forums/topic/how-to-bulk-set-parent-cpt/
Here's my adapted code:
<?php
require "wp-config.php";
$args = array( 'post_type' => 'drill', 'posts_per_page' => -1 );
$childs = get_posts($args);
foreach ($childs as $child) {
$belongs_id = get_post_meta( $child->ID, 'wpcf-import-belongs-to-id', true );
if ( $belongs_id != '' ) {
$parent_value = get_post_meta($child->ID, 'wpcf-import-belongs-to-id', true);
update_post_meta($child->ID, '_wpcf_belongs_boat_id', $parent_value);
echo "Child ID" . $child->ID . " parent_value" . $parent_value;
}
}
?>
As the post linked above details, I've got that function in a file called import.php so it can be run at will.
The function runs, but I think something is wrong
update_post_meta($child->ID, '_wpcf_belongs_boat_id', $parent_value);
- it never actually seems to link to the boat?
Any ideas on how I could sort it- perhaps my meta field is incorrect?
Ah thanks, that helped.
Finished function for anyone who comes across this:
$args = array( 'post_type' => 'drill', 'posts_per_page' => -1 );
$childs = get_posts($args);
foreach ($childs as $child) {
$belongs_id = get_post_meta( $child->ID, 'wpcf-import-belongs-to-id', true );
if ( $belongs_id != '' ) {
$parent_value = get_post_meta($child->ID, 'wpcf-import-belongs-to-id', true);
toolset_connect_posts( 'boat-drill', $parent_value , $child->ID );
update_post_meta( $child->ID, 'wpcf-import-belongs-to-id', '' );
echo "CHILD ID " . $child->ID . " <---> PARENT ID " . $parent_value . "<br/>";
}
}