With the next code, in the WordPress admin, I can generate the value of the field "email-comercial-asociado" depending on the relationship field "comercial-expediente". But now, I want to do the same but when creating the post from a front-end form.
add_action( 'save_post', 'copy_relation_to_field_email_for_recomendado', 100, 3 );
function copy_relation_to_field_email_for_recomendado( $post_id, $post, $update ) {
if ( $post->post_status == 'publish' && $post->post_type == 'recomendado' ) {
$recomendado_id = $post_id;
// Get ID from CPT "comercial" parent of CPT "recomendado"
$comercial_id = toolset_get_related_post( $recomendado_id, 'comercial-expediente', 'parent');
$author_comercial_id = get_post_field( 'post_author', $comercial_id );
$author_coemrcial_email = get_the_author_meta( 'email', $author_comercial_id );
// Update custom field
update_post_meta( $post_id, 'wpcf-email-comercial-asociado', $author_coemrcial_email );
}
}
The first code run ok, but I modify it to use it with the front-end form and it doesn't work:
But I modify it to use it from the form and it doesn't work:
add_action('cred_save_data', 'copy_relation_to_field_email_for_recomendado_frontend',999,2);
function copy_relation_to_field_email_for_recomendado_frontend($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==335)
{
if (isset($_POST['@comercial-expediente.parent']))
{
$recomendado_id = $post_id;
// Cogemos el ID del CPT comercial padre del recomendado
$comercial_id = toolset_get_related_post( $recomendado_id, 'comercial-expediente', 'parent');
$author_comercial_id = get_post_field( 'post_author', $comercial_id );
$author_coemrcial_email = get_the_author_meta( 'email', $author_comercial_id );
// add it to saved post meta
add_post_meta($post_id, 'wpcf-email-comercial-asociado', $author_coemrcial_email);
}
}
}
I'm not seeing any reason why this shouldn't work.
Would you mind allowing me to have admin access to the site as well as a link to the page where you are testing this so that I can do a firsthand check ?