[Resolved] How to copy the content of a custom field to another field on wordpress
This thread is resolved. Here is a description of the problem and solution.
Problem:
How can I copy the content of the custom field "Descripción Principal" to the default field "Entry Content" in those entries that are only of type “Patologías".
The customer enters text in the "Descripción Principal" field. But it turns out that in the entry listings you can only choose the title of the entry and the content of the entry (Astra Theme) I know it would be easier for the customer to enter text in the body of the entry, but he doesn't want it that way. So I thought that if you can copy the text from one field to another at the time of creating the entries, it would be solved. That's taking into account that it should only apply to a specific type of entry "Patologías".
How can I copy the content of the custom field "Descripción Principal" to the default field "Entry Content" in those entries that are only of type “Patologías".
Could you please elaborate a little more on this issue, so I can better understand? When you say 'entry content' field, what exactly info is this? Sometimes screenshots illustrating what you mean may go a long way to clarify what words fail to transmit, so if you like, make use of “upload an image” below the comment area.
The customer enters text in the "Descripción Principal" field. But it turns out that in the entry listings you can only choose the title of the entry and the content of the entry (Astra Theme) I know it would be easier for the customer to enter text in the body of the entry, but he doesn't want it that way. So I thought that if you can copy the text from one field to another at the time of creating the entries, it would be solved. That's taking into account that it should only apply to a specific type of entry "Patologías".
Hi Mateus, Finally and thanks to Saint ChatGTP 4. 🙂
He gave me the solution that works perfectly.
This is the code :
------------------------------------------------------------------------------------------------------------------------------
function copy_description_to_excerpt($post_id, $post, $update) {
// Verificar si el tipo de entrada es "Patologías" (slug: patologia)
if (get_post_type($post_id) != 'patologia') {
return;
}
// Verificar que el campo "Descripción Principal" exista
$main_description = get_post_meta($post_id, 'wpcf-descripcion-principal', true);
if (!$main_description) {
return;
}
// Evitar el bucle infinito
remove_action('save_post', 'copy_description_to_excerpt', 10);
// Comprobar si el contenido del campo "Descripción Principal" ha cambiado
$current_excerpt = $post->post_excerpt;
if ($main_description != $current_excerpt) {
// Actualizar el campo Extracto con el contenido del campo "Descripción Principal"
wp_update_post(array(
'ID' => $post_id,
'post_excerpt' => $main_description,
));
}
// Volver a añadir la función al hook save_post
add_action('save_post', 'copy_description_to_excerpt', 10, 3);
}
add_action('save_post', 'copy_description_to_excerpt', 10, 3);
-----------------------------------------------------------------------------------------------------------------------------------