Skip Navigation

[Resolved] Get data from Custom Fields and update other posts with it

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I get values from one post and update another post with them when I create a new post?

Solution:
It's Custom Code, which we cannot provide but explain:
https://toolset.com/forums/topic/copy-data-from-one-set-of-fields-to-another-field/#post-612359

This support ticket is created 6 years, 3 months ago. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by Beda 6 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#612280

Hi

Ive got some custom fields for example
wpcf-tag2, wpcf-tag3, wpcf-tag5, wpcf-tag6 (15 in total)

These fields hold one word each, What I am trying to do is take the content out of the those fields and put them into another field called keywords separated by a comma

For example
wpcf-tag2 = Blue
wpcf-tag3 = Yellow
wpcf-tag4 = Bike

would then copy into my field keywords like
blue, yellow, bike

Is this possible please.

#612359

Yes, this is possible.

But, you need complex PHP Custom code to achieve this.

1. You will craft a custom code that get_post_meta() the values of all fields, and update_post_meta() a new value to another field.
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Function_Reference/update_post_meta

2. How you perform the "junction" of the values, is entirely up to you, I would look at string operators and implode:
hidden link
hidden link

3. You append your custom code to either save_post() action or cred_save_data().
The first is when you save the post in the WP Admin, the second for when you do that with a CRED Form.
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

If you need some samples to work with, you may look at this Pastebin:
https://pastebin.com/u/bedas

There are a few good examples on these codes used with CRED.

#612378

Hi!

Thanks, Ive been having a look.

Would it not be possible to bring the field group out and then save that data into the new field?

But then Im unsure how I would trigger it? Or could I duplicate the entries?

So when the user fills out the cred form, could the form save the entries to the keyword field as well as the individual
So for I fill out the form
I input into the fields
wpcf-tag2 = Blue
wpcf-tag3 = Yellow
wpcf-tag4 = Bike

Those wpcf-tag fields keep the data, but the form also takes the data and duplicates it into wcpf-keyword or even duplicates the entries into a tag taxonomy called keyword-tags or something?

Just thinking how I would action it, unless it was upon the form submission?

#612380

Yes, that is exactly what I elaborated here:
https://toolset.com/forums/topic/copy-data-from-one-set-of-fields-to-another-field/#post-612359

You will need Custom PHP code to achieve this.
When you submit the Form, the PHP code will ensure that you can grab the values from Those wpcf-tag fields, they will keep their data, and you will perform the needed manipulation with the data.

Then, your PHP code will update this new value to any field you like.

The PHP Code snippets I shared on my pastebin are basically doing just that.
Look for example at this snippet:
https://pastebin.com/2BTbVbcs

I update a post title with a value from a custom field.
Just like this you can update anything else, with the API elaborated above, and get it from any other field as well.

Please let me know if you need further help on this.

#612389

Hello

So would my code be like

function my_save_data_action($post_id, $form_data){
            // Change your CRED Form "ID" accordingly below
            if ($form_data['id']==12){
               
                //Declare the content of your variables, change "your_custom_field_slug" accordingly
                $custom_title = get_post_meta( $post_id, 'wpcf-tag1', 'wpcf-tag3', 'wcpf-tag4', true );
               
                //collect data and define new title
                $my_post = array(
                    'ID'               => $post_id,
                    'wcpf-keywords'   => $custom_title,
                 
                   
                );
               
                // Update the post into the database
                wp_update_post( $my_post );
               
            }
        }
    add_action('cred_save_data', 'my_save_data_action',10,2); 

Thank you

#612397

Almost.

This will not work thou:

get_post_meta( $post_id, 'wpcf-tag1', 'wpcf-tag3', 'wcpf-tag4', true );

The related Document of this WordPress API is here:
https://developer.wordpress.org/reference/functions/get_post_meta/

The key must be a String, it cannot be an array or similar, hence it must be one value only.

Your code needs to get_post_meta() for each single field at once.
Populate a new $variables (like $custom_title in the example) with what you get from the get_post_meta() for each field.

Then, as elaborated to update a Post field, it's enough to run update_post_meta()
https://developer.wordpress.org/reference/functions/update_post_meta/

No need to run the whole chunk of wp_update_post().
It's enough to gather the information in a $variable, and then update_post_meta() the new field with this value.

A script that would put together the value of 3 fields, and update a 4th with this new value, is like this:

$variable_one = get_post_meta($post_id, 'first-field-slug', true );
$variable_two = get_post_meta($post_id, 'second-field-slug', true );
$variable_three = get_post_meta($post_id, 'third-field-slug', true );
$new_variable = $variable_one . ', ' . $variable_two . ', ' . $variable_three;
update_post_meta($post_id, 'new-field-slug', $new_variable);

This will update a new field with comma separated values of the 3 previous fields.

There are better ways to achieve this, but this gives a general idea how to get/post information from/to the database with the two functions elaborated.

This is custom code, to work with these API's I suggest to consider the Documentation - especially of WordPress if you work with their API as we do here.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.