I was provided the following code by Toolset Support to run in instances where I have the parent post ID, the child posts IDs, and the relationship slug.
$slug = 'your-relationship-slug';
$parent = 1234;
$children = array( 1, 2, 3, 4, 5 );
// you should not edit below this line
foreach( $children as $child ) {
toolset_connect_posts( $slug, $parent, $child );
}
The details of this post can be found here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Now I am seeking to to automate the assignment of child Toolset Types custom posts to parent Toolset Types custom posts, where both parent and child share the same unique Category Taxonomy, in a one to many relationship.
I would like to modify the above code snippet to the following. I am hoping to have it running in the background so that my child posts can automatically be assigned to parent posts of the same category.
$parent_category_slug = 'parent-category-slug';
$child_category_slug = 'child-category-slug';
$slug = 'your-relationship-slug';
$parent = array(of all parent posts by $parent-category-slug);
$children = array(of child posts where $child_category_slug = $parent_category_slug);
foreach ($children as $child) {
toolset_connect_posts($slug, $parent, $child);
}
Can someone please help me to fix this code snippet so that it will help me to achieve my objective? I would really appreciate this!
Addition:
This script should only act upon those Custom Posts that do not yet have a Parent / Relationship assigned to them (of course).
Thx
This isn't a trivial thing, it's a complex, custom application that stretches the limits of our support forum. I can point you to examples, but the bulk of the code is your responsibility.
$parent_category_slug = 'parent-category-slug';
$child_category_slug = 'child-category-slug';
How to query for posts in a specific post type, using a specific taxonomy term:
https://codex.wordpress.org/Class_Reference/WP_Query
There are many many examples to review there.
When to trigger this code is another matter. The WordPress API offers events like save_post, which can be triggered any time a post is updated. That might be your best option:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
As always, there is the contractors portal available for you to link up with independent professionals:
https://toolset.com/contractors
There's also a connection for smaller jobs here: https://codeable.io/developers/toolset/
Thanks for pointing me in the right direction - it was a long shot but I should be able to work on it from here!
My issue is resolved now. Thank you!