Skip Navigation

[Resolved] [continued] Connect multiple child posts to parent in one move

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to automatically connect multiple posts in a M2M relationship. The GUI lets me connect each post manually, but I would like to automate the process. I would like to query all the posts in one custom post type, which have one specific taxonomy term, and relate those posts to 3 specific posts from another custom post type.

Solution:
Create a backup of your site and database first. Then insert this code in functions.php. This code creates a shortcode which calls a custom get_posts query to get the posts in one specific category, and uses the toolset_connect_posts API to loop over each of those posts and connects it to all 3 posts in the other custom post type. Place the shortcode on a page, then load the page once in the front-end. Finally remove the code from functions.php and remove the shortcode from the page. Custom code follows:

add_shortcode('ts_update_db_relationships', 'ts_update_db_relationships_func');
function ts_update_db_relationships_func($args) {
    $pid = 34596;
    $field = '_ts_cc_wpv_manual_rel_assignment';
    $is_updated = get_post_meta( $pid, $field, true);
    if( $is_updated != 1 ) {
        $tArgs = array(
            'post_type' => 'tecaj',
            'numberposts' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'tip-tecaja',
                    'field' => 'slug',
                    'terms' => 'skupinski-tecaji',
                ),
            ),
        );
        $tPosts = get_posts($tArgs);
        $tIds = array();
        foreach( $tPosts as $tPost ) {
            $tIds[] = $tPost->ID;
        }
        $cIds = array(34821, 34820, 34814);
 
        foreach($tIds as $tId) {
            foreach($cIds as $cId) {
                toolset_connect_posts( 'tecaj_cena', $tId, $cId );
            }
        }
 
 
 
        wp_reset_query();
        update_post_meta( $pid, $field, 1 );
    }
    return '';
}

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://codex.wordpress.org/Template_Tags/get_posts

This support ticket is created 6 years, 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by matijaK 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#911695

[this post continues from the closed topic at https://toolset.com/forums/topic/connect-multiple-child-posts-to-parent-in-one-move/ ]

Hi Beda,

sorry for the late reply - please open the private response so I can share the access info with you.
The posts I would require to be connected are
- all 3 prices (post type CENE) are connected to
- all of the courses (post type TEČAJI) stored under the category (tip-tecaja) group courses (skupinski-tecaji - 118 posts).

I'm sorry everything is in Slovenian, but this site is only meant for the local market.

Thanks for your help!

#911854

Hi, private reply fields are enabled for your next reply.

#912603

Is there an IP restriction or some other security system on the site? When I visit the URL, I see "Forbidden."

#912819

I apologize,
I guess there was some IP blocking enabled trough Wordfence.
Should work now, I tested with a US VPN.

#913110

I still see "Forbidden", sorry! If you'd like to provide a backup of the site, I can work from that. You can use the Duplicator plugin to create a clone, or you can create a zip file containing a SQL dump from your database, your themes folder, and your plugins folder. Then post a link where I can download those files.

#913615

Okay I have a custom script that should be helpful here.

*First - make a backup of your database so you can revert if necessary. I tested this on my local environment without any problems, but it's always a good idea to backup*

Add this code to functions.php:

add_shortcode('ts_update_db_relationships', 'ts_update_db_relationships_func');
function ts_update_db_relationships_func($args) {
	$pid = 34596;
	$field = '_ts_cc_wpv_manual_rel_assignment';
	$is_updated = get_post_meta( $pid, $field, true);
	if( $is_updated != 1 ) {
		$tArgs = array(
			'post_type' => 'tecaj',
			'numberposts' => -1,
			'tax_query' => array(
				array(
					'taxonomy' => 'tip-tecaja',
					'field' => 'slug',
					'terms' => 'skupinski-tecaji',
				),
			),
		);
		$tPosts = get_posts($tArgs);
		$tIds = array();
		foreach( $tPosts as $tPost ) {
			$tIds[] = $tPost->ID;
		}
		$cIds = array(34821, 34820, 34814);

		foreach($tIds as $tId) {
			foreach($cIds as $cId) {
				toolset_connect_posts( 'tecaj_cena', $tId, $cId );
			}
		}



		wp_reset_query();
		update_post_meta( $pid, $field, 1 );
	}
	return '';
}

Add this code to functions.php, then insert the shortcode on any Page:

[ts_update_db_relationships]

Load the page once on the front-end of the site to trigger the update. Then delete the shortcode from the Page, and delete the code from your functions.php file.

Edit the Cena posts in wp-admin - they should have at least 118 associated Tecaj posts. If it's not correct, revert using your database backup and we can re-evaluate.

#914168

Hi Christian,

thanks - this worked perfectly! And a good idea for future features I might require as well.

Thanks so much.
regards;
Matija