Skip Navigation

[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".

Solution:

https://toolset.com/forums/topic/how-to-copy-the-content-of-a-custom-field-to-another-field-on-wordpress/#post-2588765

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 4 replies, has 2 voices.

Last updated by jose-franciscoV 1 year, 10 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2585905

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".

#2586239

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Thank you for contacting our support.

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.

Thank you, please let us know.

Mateus

#2586621
2023-04-04 12_23_38-Sticky Notes.png

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".

#2587129

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Thank you for the clarifications.

In this case, what might work for you is the 'Inline Fields' button inside the WP blocks: https://toolset.com/course-lesson/display-dynamic-sources-inside-all-text-blocks/. With this, you can dynamically select the value of core and custom fields to display in a different block.

Please take a look on this documentation and check if the proposed solution applies to the 'entry content' block.

Looking forward to your reply. Thanks!

#2588765

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);
-----------------------------------------------------------------------------------------------------------------------------------