Tell us what you are trying to do?
I have 2 CPT: Editions and Subscribers related One to Many (One edition could have many subscribers)
In Editions I have 2 custom fields: "start date" and "end date"
In Subscribers I have the "mirror" custmo fields "start date mirror" and "end date mirror"
I created a form to add subscribers to an edition
I created a template for editions with a view with a button with a link the page with the form to add subscribers.
In that form (add subscriber) the Field Default Value for start date mirror is this shortcode:
[types field='start-date' output='normal' style='text' format='d/m/Y' ][/types]
So I copied the start date from parent cpt
So far so good...
But.
If I edit the cpt edition and change the start date and/or end date, in the subscribers mirror field (start date mirror or end date mirror ) it doesn't update.
What I need is update the start date mirror and/or end date mirror fields when the parent is modified.
Is there any documentation that you are following?
I see the following topic but I didn't understand completely
Hello. Thank you for contacting the Toolset support.
Do you mean that when you edit the parent "Editions" from backend admin - you want to update the child post fields start date mirror and/or end date mirror?
I mean when I edit the parent "Editions" using a Post form (Form type Edit existing content), from frontend.
I want to update the childpost fields start date mirror and/or end date mirror.
To update the child post custom fields when you update the parent, you can do that using the Toolset Form hook "cred_save_data":
For example:
add_action( 'cred_save_data', 'func_update_child_post_fields_when_submit_parent', 10, 3 );
function func_update_child_post_fields_when_submit_parent( $post_id, $form_data ){
// Edit these as required
$relationship_slug = 'replace-your-post-relationship-slug';
// parent post fields
$start_date = 'wpcf-start-date'; // note wpcf- prefix for Types fields
$end_date = 'wpcf-end-date';
// child post fields
$start_date_mirror = 'wpcf-start-date_mirror'; // note wpcf- prefix for Types fields
$end_date_mirror = 'wpcf-end-date_mirror';
$form_ids = array( 99999); //replace form ids
if ( in_array( $form_data['id'],$form_ids ) ) { // Edit for form ID
// get child posts of this parent
$children = toolset_get_related_posts(
$post_id,
$relationship_slug,
array(
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_id',
'role_to_return' => 'child'
)
);
// get the parent custom field value
$parent_start_date_value = get_post_meta( $post_id, $start_date , true );
$parent_end_date_value = get_post_meta( $post_id, $end_date , true );
// update the children with this field value
foreach ($children as $key => $child) {
update_post_meta( $child, $start_date_mirror , $parent_start_date_value );
update_post_meta( $child, $end_date_mirror , $parent_end_date_value );
}
}
}
Where:
- Replace 99999 with your original parent post edit form ID
- Replace 'replace-your-post-relationship-slug' with your original post relationship slug
- Replace the custom date fields slugs where applicable/required
My site is in spanish language
The CPTs are:
Cursos << Fechas << Inscriptos
My real field and slugs names are: (they are in spanish)
Relationships slugs are:
curso-fecha
fecha-inscripto
Field names are:
In Fechas cpt:
fecha-inicio
fecha-fin
In Insriptos CPT
fecha-inicio-esp
fecha-fin-esp
So, I adapt your snippet like this:
// Paso datos de la fecha de un curso a campos "espejo" dentro del CPT de inscriptos
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_action( 'cred_save_data', 'func_update_child_post_fields_when_submit_parent', 10, 3 );
function func_update_child_post_fields_when_submit_parent( $post_id, $form_data ){
// Edit these as required
$relationship_slug = 'fecha-inscripto';
// parent post fields
$fecha_inicio = 'wpcf-fecha-inicio'; // note wpcf- prefix for Types fields
$fecha_fin = 'wpcf-fecha-fin';
// child post fields
$fecha_inicio_esp = 'wpcf-fecha-inicio-esp'; // note wpcf- prefix for Types fields
$fecha_fin_esp = 'wpcf-fecha-fin-esp';
$form_ids = array(4464); //replace form ids
if ( in_array( $form_data['id'],$form_ids ) ) { // Edit for form ID
// get child posts of this parent
$children = toolset_get_related_posts(
$post_id,
$relationship_slug,
array(
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_id',
'role_to_return' => 'child'
)
);
// get the parent custom field value
$parent_fecha_inicio_value = get_post_meta( $post_id, $fecha_inicio , true );
$parent_fecha_fin_value = get_post_meta( $post_id, $fecha_fin , true );
// update the children with this field value
foreach ($children as $key => $child) {
update_post_meta( $child, $fecha_inicio_esp , $parent_fecha_inicio_value );
update_post_meta( $child, $fecha_fin_esp , $parent_fecha_fin_value );
}
}
}
I think that this question is wrong....
The original question was "Update child field when update parent field".
But this problem is not going to be a problem anymore. I'm going to open a new ticket.