Skip Navigation

[Resolved] I need help to copy many to many items

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Minesh 8 months ago.

Assisted by: Minesh.

Author
Posts
#2701925

I have a one to many design that is Companies have Collaborators. Sometimes a client creates a new company and has to create as many collaborators to the new companies.

I need help to create an algorithm that given two Company slugs (source, target), gets all the collaborators from source and copies to the target.

Do you have anything like that?

#2701928

Does this algorithm do the job?

function copy_collaborators($source_slug, $target_slug) {
// Get the source and target company post IDs
$source_company = get_page_by_path($source_slug, OBJECT, 'company');
$target_company = get_page_by_path($target_slug, OBJECT, 'company');

if (!$source_company || !$target_company) {
return 'Invalid company slugs provided.';
}

$source_company_id = $source_company->ID;
$target_company_id = $target_company->ID;

// Get collaborators of the source company
$args = array(
'post_type' => 'collaborator',
'meta_query' => array(
array(
'key' => 'company',
'value' => $source_company_id,
'compare' => '='
)
)
);

$collaborators = get_posts($args);

foreach ($collaborators as $collaborator) {
// Duplicate collaborator
$new_collaborator_id = wp_insert_post(array(
'post_title' => $collaborator->post_title,
'post_content' => $collaborator->post_content,
'post_status' => 'publish',
'post_type' => 'collaborator'
));

// Copy custom fields and associate with target company
if ($new_collaborator_id) {
$custom_fields = get_post_custom($collaborator->ID);
foreach ($custom_fields as $key => $value) {
update_post_meta($new_collaborator_id, $key, maybe_unserialize($value[0]));
}

// Associate with target company
update_post_meta($new_collaborator_id, 'company', $target_company_id);
}
}

return 'Collaborators copied successfully.';
}

// Usage
$result = copy_collaborators('source-company-slug', 'target-company-slug');
echo $result;

#2702217

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Actually - Toolset offers post-relationship API using which you can get the related posts, connect post or disconnect the posts in relationship:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

As I understand you have set of posts and you want to copy those posts and establish post-relationship between them. If that is correct you can use the post-relationship API functions where required.

You can also query the related posts using WP_Query:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

Toolset offers limited support for such custom code. If you need any help with your custom code you can always contact our certified partners:
- https://toolset.com/contractors/

#2702516

Thank you for your answer. For those who cross by, I have made a method to be used in the WP API that does the job. Company (empresa) has many Collaborators (colaborador) and this code copies the collaborators from one company to another given their slugs.

public function copy_collaborators($request) {
$source_slug = $request->get_param('source_slug');
$target_slug = $request->get_param('target_slug');

// Get the source and target company post IDs
// debug($request->get_params());
// debug($source_slug, $target_slug);
$source_company = get_page_by_path($source_slug, OBJECT, 'empresa');
$target_company = get_page_by_path($target_slug, OBJECT, 'empresa');

if (!$source_company) {
return new WP_REST_Response(['error' => 'Invalid source company slug provided: ' . $source_slug], 404);
}

if (!$target_company) {
return new WP_REST_Response(['error' => 'Invalid target company slug provided: ' . $target_slug], 404);
}

$source_company_id = $source_company->ID;
$target_company_id = $target_company->ID;

// Validate relationship
$source_main_account = get_post_meta($source_company_id, 'wpcf-main-account', true);
$target_main_account = get_post_meta($target_company_id, 'wpcf-main-account', true);

if ($source_main_account && $target_main_account) {
// Both are subaccounts, check if they belong to the same parent account
if ($source_main_account !== $target_main_account) {
return new WP_REST_Response([
'error' => 'Source and target subaccounts must belong to the same parent account',
'source_slug' => $source_slug,
'target_slug' => $target_slug,
'source_main_company' => $source_main_account,
'target_main_company' => $target_main_account,
], 400);
}
} elseif ($source_main_account && !$target_main_account) {
// Source is a subaccount and target is an account, invalid
return new WP_REST_Response([
'error' => 'Cannot copy collaborators from a subaccount to an account',
'source_slug' => $source_slug,
'target_slug' => $target_slug,
'source_main_company' => $source_main_account,
'target_main_company' => $target_main_account,
], 400);
} elseif (!$source_main_account && $target_main_account) {
// Source is an account and target is a subaccount, valid
// Check if the target's main account is the source company
if ($target_main_account !== $source_company->post_title) {
return new WP_REST_Response([
'error' => 'Target subaccount must belong to the source account',
'source_slug' => $source_slug,
'target_slug' => $target_slug,
'source_main_company' => $source_company->post_title,
'target_main_company' => $target_main_account,
], 400);
}
} else {
// Both are accounts, invalid
return new WP_REST_Response(['error' => 'Cannot copy collaborators from an account to another account'], 400);
}

// Get collaborators of the source company
$collaborators = toolset_get_related_posts($source_company_id, 'empresa_colaborador', [
'query_by_role' => 'parent',
'return' => 'post_object',
'posts_per_page' => -1
]);

$num_source_collabs = count($collaborators);
$num_collabs_created = 0;
$num_collabs_skipped = 0;
$collabs_skipped = [];

foreach ($collaborators as $collaborator) {
// Retrieve the author of the source collaborator
$author_id = $collaborator->post_author;
$author = get_userdata($author_id);
$author_display_name = $author->display_name;

// Check if the collaborator already exists for this user and target company
$existing_collabs = get_posts([
'post_type' => 'colaborador',
'posts_per_page' => -1,
'author' => $author_id,
]);

$found_parent = null;
foreach ($existing_collabs as $existing_collab) {
$parent = toolset_get_related_posts($existing_collab->ID, 'empresa_colaborador', [
'query_by_role' => 'child',
'return' => 'post_object'
]);
if ($parent && $parent[0] && $parent[0]->ID == $target_company_id) {
$found_parent = $parent[0];
break;
}
}

// If a collaborator already exists, skip creating a new one
if ($found_parent) {
$num_collabs_skipped++;
$collabs_skipped[] = [
'collaborator_id' => $collaborator->ID,
'collaborator_name' => $collaborator->post_title,
'reason' => 'Collaborator already exists for target company'
];
continue;
}

// Construct the new title
$new_title = $author_display_name . ' - ' . $target_company->post_title;

// Duplicate collaborator with the updated author and title
$new_collaborator_id = wp_insert_post(array(
'post_title' => $new_title,
'post_content' => $collaborator->post_content,
'post_status' => 'publish',
'post_type' => 'colaborador',
'post_author' => $author_id
));

// Copy custom fields and associate with target company
if ($new_collaborator_id) {
$custom_fields = get_post_custom($collaborator->ID);
foreach ($custom_fields as $key => $value) {
update_post_meta($new_collaborator_id, $key, maybe_unserialize($value[0]));
}

// Connect the new collaborator with the target company
toolset_connect_posts('empresa_colaborador', $target_company_id, $new_collaborator_id);

$num_collabs_created++;
}
}

$response_data = [
'source_company' => [
'id' => $source_company->ID,
'name' => $source_company->post_title,
'slug' => $source_company->post_name
],
'target_company' => [
'id' => $target_company->ID,
'name' => $target_company->post_title,
'slug' => $target_company->post_name
],
'num_source_collabs' => $num_source_collabs,
'num_collabs_created' => $num_collabs_created,
'num_collabs_skipped' => $num_collabs_skipped,
'collabs_skipped' => $collabs_skipped,
'total_collabs_processed' => $num_source_collabs
];

return new WP_REST_Response($response_data, 200);
}