Skip Navigation

[Resolved] Bulk set parent post

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 1 voice.

Last updated by davidS-53 5 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1202152

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?

#1202888

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/>";
        }
    }