Hi
I have a Parent Post "client" and two Child posts "note, attachment" connected with One to many relationship. And these child posts are being created using webhooks. So I want to know how I can connect these child posts with Parent Posts. In the Webook, I get the ID of the parent Post and also I get a unique value of a custom field which is unique for each parent post that we call as "Patient ID" So, I have two unique IDs that can be used to connect parent Post with Child post, But I think Parent Post ID is the right thing to use to connect with child post. But I am not sure how I can do it.
So please help me what meta key I can use to map the Parent Post ID And connect the newly created child post with parent posts.
Hi, I didn't understand how exactly you got my point, but for better understanding, Please watch the video of what exactly I wanna do.
hidden link
Waiting to hear from you.
In the latest Toolset Types plugin, it does not use custom field _wpcf_belongs_xxxx to store the post type relationships, you need to follow the document I mentioned above to use API function toolset_connect_posts() to setup the post type relationship:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Connects posts within a given post relationship.
In your case, it needs custom codes, for example:
1) After Child post was created, use action hook save_post to trigger a PHP function:
https://developer.wordpress.org/reference/hooks/save_post/
2) In this PHP function get the custom field _wpcf_belongs_xxxx field value
https://developer.wordpress.org/reference/functions/get_post_meta/
And use toolset_connect_posts() function to setup the post type relationships between parent/child posts
Hi, thanks for checking the video and understanding my issue. Unfortunately, I am not an expert in PHP And Can't understand how I can make it work, So If you can please share any sample code similar to my requirements then Maybe I can follow up and make it work. Please help me.
Thank you.
Hi,
So this is something i found similar to your recommended option, but its still not working.
Can you please help me make this code working?
function save_device_parent($post_id) {
if (get_post_type($post_id) == 'device' && isset($_GET['parent'])) {
$parent = (int)$_GET['parent'];
update_post_meta($post_id, '_wpcf_belongs_system_id', $parent);
}
}
add_action( 'save_post', 'save_device_parent');
Since it is a custom codes problem, please provide a test site with the same problem, also point out where I can edit your custom PHP codes, I need a live website to test and debug, thanks
Thanks for the details, I have done below modifications in your website:
1) Edit your theme file "functions.php", add below codes:
add_action( 'save_post', 'assign_parent_patient_func', 10,3 );
function assign_parent_patient_func( $post_id, $post, $update ) {
// Only want to set if this is a new post!
/* if ( $update ){
return;
} */
// Only set for post_type = post!
if ( !in_array($post->post_type, array('qev-attachment', 'note')) ) {
return;
}
$patient_id = get_post_meta($post_id, 'wpcf-npatient-id', true);
if($patient_id){
$client_ids = get_posts( array(
'post_type' => 'client',
'meta_key' => 'wpcf-patient-id',
'meta_value'=> $patient_id,
'fields' => 'ids'
));
if(isset($client_ids[0])){
$client_id = $client_ids[0];
if($post->post_type == 'qev-attachment'){
toolset_connect_posts('client-attachment', $client_id, $post_id);
}
if($post->post_type == 'note'){
toolset_connect_posts('client-note', $client_id, $post_id);
}
}
}
}
2) Test the result like this:
Create a new "Note" post, for example:
hidden link
In field "Patient ID" fill value: 298244000001872192
Save the post, I can see it works fine, it connect the patient post "testing Client" correctly.
Hi, Thanks for your effort, I tested but the function doesn't work for child posts being created via incoming webhook data. Please review and help me get this fixed.
and request you to hide these vidoes please
hidden link
hidden link
I think there is a misunderstanding, according to our support policy, we don't provide other plugin support.
The custom codes I provide above works for WordPress built-in action hook "save_post":
https://developer.wordpress.org/reference/hooks/save_post/
Have you tried as I mentioned above?
https://toolset.com/forums/topic/need-help-to-assign-child-posts-to-parent-post-created-using-webhook/#post-2127389
2) Test the result like this
Can you confirm it?
It seems that the plugin "WP webhook" does not trigger action hook "save_post", you will need to check it with "WP webhook" plugin author for it, is there any similar WordPress action hook you can use after new post created, then customize the PHP codes according to their documents.
Hi Luo,
So, I had modified your code and also I made changes on my Custom Posts Fields, Previously I had created multiple duplicate fields for each Post type to store Patient ID, But Now I created one fields group applicable for all 3 custom Post types and connected it and then modified it to make it work but still, the function works only for posts being created from admin panel or Using Toolset Posts Forms. But not with Webhook-based data. Below is the code.
Also, I talked to the Webhook Plugin Development team, and they replied below
I just checked the video and it seems like the logic that is used by one of your plugins (for actually connect a child post with a parent post), isn't using the WordPress standard post_parent field.
Moreover, it looks like it's using some custom post meta entry to relate the post to also allow multiple connections.
Since that's the case, I suggest you check what value is actually saved within the database for that specific post as post meta - there you should find further details on how the third-party plugin is creating the relation.
Once you have that, you can use our manage_post_meta argument to update that relation
Please can you explain how these parent and child posts are connected, as the relationship is not the default WP Relation option otherwise, Webhook Developer told me to use "post-parent" argument that passes the parent post id and connect it. but that doesn't work due to no default relationship found.
add_action( 'save_post', 'assign_parent_patient_func', 10,3 );
function assign_parent_patient_func( $post_id, $post, $update ) {
// Only want to set if this is a new post!
/* if ( $update ){
return;
} */
// Only set for post_type = post!
if ( !in_array($post->post_type, array('qev-attachment', 'note')) ) {
return;
}
$patient_id = get_post_meta($post_id, 'wpcf-patient-id', true);
if($patient_id){
$client_ids = get_posts( array(
'post_type' => 'client',
'meta_key' => 'wpcf-patient-id',
'meta_value'=> $patient_id,
'fields' => 'ids'
));
if(isset($client_ids[0])){
$client_id = $client_ids[0];
if($post->post_type == 'qev-attachment'){
toolset_connect_posts('client-attachment', $client_id, $post_id);
}
if($post->post_type == 'note'){
toolset_connect_posts('client-note', $client_id, $post_id);
}
}
}
}
Please have a look to my first answer above:
https://toolset.com/forums/topic/need-help-to-assign-child-posts-to-parent-post-created-using-webhook/#post-2126293
In the latest Toolset Types plugin, it does not use custom field _wpcf_belongs_xxxx to store the post type relationships, you need to follow the document I mentioned above to use API function toolset_connect_posts() to setup the post type relationship
The your website database, there are two tables are for storing the post type relationships:
- wp_toolset_associations
- wp_toolset_relationships
You need to follow our document to setup the post type relationships with API function toolset_connect_posts():
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Connects posts within a given post relationship.
See the example codes I provided above:
https://toolset.com/forums/topic/need-help-to-assign-child-posts-to-parent-post-created-using-webhook/#post-2127565
My issue is resolved now. Thank you!