Skip Navigation

[Resolved] Script to populate M2M relationship

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

Problem:

Convert legacy post relationships to current M2M relationships using custom PHP codes.

Solution:

Here is an example PHP codes:

https://toolset.com/forums/topic/script-to-populate-m2m-relationship/#post-1571557

Relevant Documentation:

This support ticket is created 4 years, 8 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/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Ian Henderson 4 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1569847

This topic refers to this old thread: https://toolset.com/forums/topic/relationship-migration-problem-no-merge-allowed/

What I know about PHP is dangerous - could I please ask for some help here to write the snippet for the first relationship to be run in custom codes?

The migrated relationship has created 'publication_authorship' between post types 'people' and 'publication'.

I have created the new M2M relationship post type 'publication-person'

However, I can't work out the syntax to loop through the posts and create the new values.

I'd need to:
- use toolset_get_related_posts to loop through 'poeple' (or 'publication_authorship'?) and get the related posts
- and for each of those results use toolset_connect_posts to create the new record in 'publication-person'

Could you please assist here?
Many thanks

#1570243

Hello,

Please elaborate the question with more details:
and for each of those results use toolset_connect_posts to create the new record in 'publication-person'

How are you going to create the new record record in 'publication-person'?
There should be some connection between between "person" post and "people" post.
Are those "person" posts using the same post title as "people" posts?

I have checked your previous thread:
https://toolset.com/forums/topic/relationship-migration-problem-no-merge-allowed/
Both the duplicator package file link and site credentials are not valid any more.

Please provide your website database dump file in below private message box, I need to test it in my localhost.

#1570449

Hi Luo

As per Nigel's post - I was using the Legacy relationships, and had one post type (authorships) to act as intermediary between people (the authors of the content) and the various publication post types.

When I ran the migration to the new relationships, Types 3 can't share the same intermediate post type, so the migration created one post type for people_authorship and then multiple post types for publication_authorship (and news_authorship, blog_authorship etc.).

I want to write a script to run in Toolset Custom Code to copy the data to each new M2M relationship post type, as Nigel suggested.

The script would loop through people_authorship and publication_authorship to get the related publications and use toolset_connect_posts to make a connection using the new people - publication relationship in the new M2M relationship post type 'Publications People'.

I would then repeat for each of the other post types to re-create the relationships between people and that post type.

#1570481

Thanks for the details, I am downloading the files, will update here if there is anything found.

#1571557

Here are what I found, you can try these:
1) Create a PHP file in your website root folder, for example "batch-merge-relationships.php", with below codes:

<?php 

define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( 'wp-load.php' );

// change settings - Start
$relationship = 'publication-person';
$parent_type = 'publication';
$child_type = 'people';
$parent_field_slug = '_wpcf_belongs_publication_id';
$child_field_slug = '_wpcf_belongs_people_id';
// change settings - End

$args = array(
		'post_type'  => 'authorship',
		'fields'	=> 'ids',
		'nopaging'	=> 'true',
/* 		'posts_per_page'	=> 1,
		'paged' => true, */
		'meta_query' => array(
			'relation' => 'AND',
			array(
				'key'     => $parent_field_slug,
				'compare' => 'EXISTS',
			),
			array(
				'key'     => $parent_field_slug,
				'compare' => 'NOT IN',
				'value'	  => array('', 0),
			),
			array(
				'key'     => $child_field_slug,
				'compare' => 'EXISTS',
			),
			array(
				'key'     => $child_field_slug,
				'compare' => 'NOT IN',
				'value'	  => array('', 0),
			),
		),
);
$query = new WP_Query( $args );
$post_ids = $query->posts;
header('Content-Type: text/plain');
foreach($post_ids as $k => $post_id){
	$parent = get_post_meta($post_id, $parent_field_slug, true);
	$child = get_post_meta($post_id, $child_field_slug, true);
	if(function_exists('toolset_connect_posts') && get_post_type($parent) == $parent_type && get_post_type($child) == $child_type){
		echo '( ' . $post_id . ')';
		if(toolset_connect_posts( $relationship, $parent, $child )){
			echo ' connected: ' . $parent . ' and ' . $child;
		}
		echo "\r\n";
	}
}

2) Open above file URLs, for example:
hidden link
You should be able to see the results.

Please backup your website database first, and you can delete that file after the migration.

#1572509

Thank you Luo! I really appreciate the assistance - this worked 100%. Really useful script for anyone who needs to convert legacy post relationships to current M2M relationships.