I have created the post type consultant and the child post type job. I need when a new job post is created the field wpcf-updated-consultant-field from the consultant post been updated with specific value.
I try to follow this documentation: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_parent_post_by_type
Here is my cred save data hook:
function consultant_data_action($post_id, $form_data) {
if ($form_data['id']==30) {
$consultant = get_post( $consultant_id ); $job = toolset_get_parent_post_by_type( $consultant, 'job' );
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);
but it is not working... What am I doing wrong?
Hi, please note that the toolset_get_parent_post_by_type API can only be used if the post relationship was created in the old (legacy) system. Post relationships created in Types 3.0+ or after migrating to new Post Relationships will not work with this API, you should use toolset_get_related_posts instead. If you are working with a legacy relationship or a migrated legacy relationship, this API is okay.
It looks like you've got a couple of issues in this code. The first issue is there is no variable $consultant_id so that's going to throw a fatal error. Second, it looks like you have your child and parent post types backwards. Assuming this is a legacy relationship or migrated legacy relationship, try this update:
function consultant_data_action($post_id, $form_data) {
if ($form_data['id']==30) {
$consultant = toolset_get_parent_post_by_type( $post_id, 'consultant' );
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);
Thank you Christian for your reply! I tried your code and also I tried:
function consultant_data_action($post_id, $form_data) {
if ($form_data['id']==30) {
$consultant = toolset_get_related_post( $job_id, array( 'consultant', 'job' ) );
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);
but neither is working. How could I get the parent id and update a parent's custom field with the new relationships system each time I am creating a new child post? Could you give me an example?
For relationships created in the new system, the code is like this:
function consultant_data_action($post_id, $form_data) {
if ($form_data['id']==30) {
$consultant = toolset_get_related_post( $post_id, 'relationship-slug', 'parent' );
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);
You must replace 'relationship-slug' with the correct relationship slug, which can be found by editing the post relationship in Toolset > Relationships. While you're there, you can see a list of all relationships. If the relationship was migrated from a legacy relationship, it will say "Migrated".
Christian I am so sorry but I cannot make it work. If the child post type is job and the parent post type is consultant my code is:
function consultant_data_action($post_id, $form_data) {
if ($form_data['id']==30) {
$consultant = toolset_get_related_post( $post_id, 'consultant-job', 'parent' );
$update = 500;
update_post_meta($consultant, 'wpcf-updated-consultant-field', $update);
}
}
add_action('cred_save_data', 'consultant_data_action',10,2);
I expect the consultant's custom field 'wpcf-updated-consultant-field' to take the value 500 after the submition of the form with the id 30 but nothing happens... I also tried replacing in the code "parent" with "consultant" but again no result... I do not understand what I am doing wrong...
I was just editing wrong functions.php file... Now it works like a charm. I can change the parent post's field when creating a child post. I tried also do the opposite but it didn't work. If we assume that a consultant has many jobs child posts and each job has the field wpcf-update-job-field, how could update this field of every job and give it the value 500 if I edit the consultant?
You can perform the child post update from the parent post by querying all the related child posts, then looping over them and updating their postmeta values. Something like this:
function reverse_consultant_data_action($post_id, $form_data) {
if ($form_data['id']==12345) {
$rels = toolset_get_related_posts(
$post_id,
'consultant-job',
'parent',
1000000,
0,
array(),
'post_id',
'child'
);
foreach($rels as $rel) {
update_post_meta($rel, 'wpcf-child-field', 500);
}
}
add_action('cred_save_data', 'reverse_consultant_data_action',10,2);
Hello Cristian,
Here is my updated code but it does not work...
function reverse_consultant_data_action($post_id, $form_data) {
if ($form_data['id']==23) {
$rels = toolset_get_related_posts(
$post_id,
'consultant-job',
'parent',
1000000,
0,
array(),
'post_id',
'child'
);
foreach($thisRel as $rel) {
update_post_meta($rel, 'wpcf-update-me', 500);
}
}}
add_action('cred_save_data', 'reverse_consultant_data_action',10,2);
The relationship slug and the custom field slug are correct, why might not be working?
Also I would like to ask you what is the number after 'parent'
Thank you
There was a typo in my code, sorry. Here's an updated version:
function reverse_consultant_data_action($post_id, $form_data) {
if ($form_data['id']==23) {
$rels = toolset_get_related_posts(
$post_id,
'consultant-job',
'parent',
1000000,
0,
array(),
'post_id',
'child'
);
foreach($rels as $rel) {
update_post_meta($rel, 'wpcf-update-me', 500);
}
}}
add_action('cred_save_data', 'reverse_consultant_data_action',10,2);
The number after "parent" is the maximum number of child posts to return. There is currently no way to tell the toolset_get_related_posts API to return all the results with no limit - a positive integer value must be supplied here. It's not ideal, and there is discussion going on about this among the team. I'm hoping this gets modified soon to behave more like a standard post query. For now, you must choose a number.
Now it works like a charm, thank you so much Cristian!!!