Skip Navigation

[Résolu] Repeating Fields Question

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

Is it possible to get all the values of a repeating custom field (for a specific post) and add them "as one" to a different custom field?

Solution:

There isn't such kind of built-in feature, it needs custom codes, for example:

https://toolset.com/forums/topic/repeating-fields-question/#post-1489695

Relevant Documentation:

This support ticket is created Il y a 4 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 4 années et 10 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1489457

Is it possible to get all the values of a repeating custom field (for a specific post) and add them "as one" to a different custom field? For example, repeating field is "wpcf-custom-field", post has 5 instances with values "red", "yellow", "green", "blue" & "white", the meta value of the other field (say "wpcf-custom-field-merged") would then be "red yellow green blue white" or better still "red, yellow, green, blue, white".

Thanks

#1489695

Hello,

There isn't such kind of built-in feature, it needs custom codes, for example, you can get the custom field "wpcf-custom-field" value as an array:
https://codex.wordpress.org/Function_Reference/get_post_meta
$single
(bool) (Optional) If true, returns only the first value for the specified meta key. This parameter has no effect if $key is not specified.

Then use above value to update another field value as what you want:
https://developer.wordpress.org/reference/functions/update_post_meta/

#1489739

Do you mean like this?

add_action('cred_save_data', 'save_data_form_1974',10,2);
function save_data_form_1974($post_id, $form_data) {
    
    if ($form_data['id']==1974) {
        
        $rfg_all_values = get_post_meta($post_id,  'wpcf-custom-field',  false);        
        update_post_meta($post_id,  'wpcf-custom-field-merged',  $rfg_all_values);
	    }
}
#1489749

I assume you are using Toolset Forms plugin to create the new post, you need to turn the PHP array into a string, for example:

add_action('cred_save_data_1974', 'save_data_form_1974',10,2);
function save_data_form_1974($post_id, $form_data) {
	$arr = get_post_meta($post_id,  'wpcf-custom-field',  false);
	$str = implode(', ', $arr);
	update_post_meta($post_id,  'wpcf-custom-field-merged',  $str);
}

More help:
lien caché

#1489759

That's fabulous Luo, thank you!