Skip Navigation

[Resolved] Script to Automate Assigning One to Many Child Posts to Parent Post by Category

This support ticket is created 5 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 4 replies, has 2 voices.

Last updated by andrewF-6 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1248073

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!

#1248321

Addition:
This script should only act upon those Custom Posts that do not yet have a Parent / Relationship assigned to them (of course).
Thx

#1248329

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/

#1248407

Thanks for pointing me in the right direction - it was a long shot but I should be able to work on it from here!

#1248411

My issue is resolved now. Thank you!