Skip Navigation

[Resolved] Append CRED single field value to custom select field

This support ticket is created 8 years, 2 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.

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 7 replies, has 2 voices.

Last updated by Beda 8 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#368722

Hi, I would like to do just that. Append a CRED single field value to the options of a custom select field after a CRED submission. Would that be possible?

Thanks!

#368904

I apologize the delay here

It is possible, you can use the CRED API to update meta_values when you submit the CRED Form.

It depends a bit what you want to do, as you could also simply display that value, without actually programmatically appending it, when you render the Fields.

Please can you let me know, what type of field (eventually also the according slugs/names of them ) you want to join together, and where you want to store the new value?

If you are comfortable with PHP you can go ahead and build the Code yourself.

Here is the DOC for this task:

CRED API:
https://toolset.com/documentation/user-guides/cred-api/#csd

Get the Fields values:
https://developer.wordpress.org/reference/functions/get_post_meta/

Update Post meta:
https://codex.wordpress.org/Function_Reference/update_post_meta

Examples:
http://pastebin.com/A2UMXVUJ
http://pastebin.com/2BTbVbcs
http://pastebin.com/KjW2y826

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#368930

It's a custom meta field created by another plugin with a name of "medafi_city". From what I understand, do I need to bring that meta field under Types' control, put it inside the related Group and set is as a select field?

I think, I am able to get the post meta with:

$city = get_post_meta( $post_id, $_POST['wpcf-city'], true);

I am not sure how I could append it to the custom dropdown though...

#369092

You use get_post_meta() to access any Post Fields in the DDBB in this syntax:

get_post_meta ( int $post_id, string $key = '', bool $single = false )

Ready to use this means:

get_post_meta ( 1, 'your-field-slug', true );

Above explained:
int $post_id ==> Post ID, must be a integer
string $key = '' ==> This is the Field's meta_key (name/slug), and must be within '' apostrophes
bool $single = false ==> Wether to return a single value, as a boolean (true, false)

Now, if you do not want to let users edit your Custom field with CRED there is no need to add it to CRED Control.
Also, Types control is only needed if you want to control this field with Types.

To update a Types Custom Field with a 3rd party Custom Field value, you can use above same PHP WordPress API.

Given the below example, I have 2 Fields one created with Types and one with another plugin.
On CRED I edit the Types Field, but not the 3rd party Field.

On Submit I want the Types Field to be updated with the Types value + The 3rd Party value.

function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        $types_field_value = get_post_meta($post_id, 'wpcf-my-types-field', true);
        $third_party_field_value = get_post_meta($post_id, 'third-party-field-slug', true);
        $new_types_field_value = $types_field_value . $third_party_field_value;

        update_post_meta($post_id, 'wpcf-my-types-field', $new_types_field_value);
    }
}
add_action('cred_save_data', 'my_save_data_action',10,2);

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#369185

I don't think that's gonna work. What you are doing here is you append the third party field value to the types field value and then update the types value. I want to keep the types value as it is and append it to the third party value.

So in that case I would just do:

$city = get_post_meta($post_id, 'wpcf-city', true);
update_post_meta($post_id, 'medafi_city', $city);

but I don't see the value there. Remember the medafi_city value is a dropdown and I would like to append the types city value in the medafi_city's select options.

#369528

Then you just do the opposite, if that 3rd Party Field is a default WordPress Post Meta, you simply change the $new_types_field_value variable so you have the info you need in there, and then you do update_post_meta() the 'third-party-field-slug' field instead.

Since that field is a dropdown, it's possible the 3rd party Software uses a Array in the DDBB for the meta_key value.

I can not control how they store the information, you would need to check in your Database, or contact the 3rd Party Software developer to know this.

The Correct syntax for update_post_meta() can be found here:
https://codex.wordpress.org/Function_Reference/update_post_meta

 <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?> 

In clear-text this means:

update_post_meta($post_id, 'medafi_city', $city);

Please do not hesitate to open a new thread if other issues or problems with Toolset arise

Thank you for your patience.

#370723
medafi_city.jpg

Ok, I sould assume that the code I sent you to my previous thread is correct.

I don't see any changes. Since I will be changind that thrird party field now, does that mean that I would need to set the field under Types control now?

Also, I had a look at the database there are a few occurences of that field inside the postmeta table but I have no idea what to make of it. I have also added a few entries myself to that field through the WordPress admin. See attahced file.

To explain how this works, the plugin offers an admin where I can create various meta fields to be used as filters for posts. You can create various meta filed types, in this case we are using a dropdown metafield called medafi_city. I can add various options for that dropdown metafiled to be used as filters. In each post on the WordPress admin, I would then select that meta field(city, in my example) and select the city from the dropdown menu of already created option values. I want to be able to do that automatically when a user submits a post with a certain city value which would initially be a Types custom field. It's that field that I want to be appending in the medafi_city dropdown values.

I know I could use a View filter for that but the View filter won't allow me to filter a certain kinf of Grid that I am using(Essential Grid)

#371205

No, as I mentioned, if you do not need to control the 3rd Party field via the Types or CRED Screens, but will do it programmatically, you will not need to add it to our controls.

Only if you want to edit the fields with Types, or, in CRED, you need to add it to our Controls.
But if the update or field handling is going to happen programmatically only, there is no need to control the Field with Types or CRED.

The Code i provided is different then yours, please see the Syntax of the Custom Field update and get parameters as per the WordPress Codex:
https://codex.wordpress.org/Function_Reference/update_post_meta

This syntax is wrong:
https://toolset.com/forums/topic/append-cred-single-field-value-to-custom-select-field/#post-368930
While this is correct:
https://toolset.com/forums/topic/append-cred-single-field-value-to-custom-select-field/#post-369185
https://toolset.com/forums/topic/append-cred-single-field-value-to-custom-select-field/#post-369528

1. I had a look at the database there are a few occurences of that field inside the postmeta table but I have no idea what to make of it

The instances you see there, are the Fields saved for different posts.
As for your field "medafi_city" you have saved it in 3 Posts ($post_id) and each has a distinct meta_value
(London, Lyon and Paris)
Each of those fields has the same meta_key, a different assigned $post_id and a different meta_key.

Depends which Post ID is passed in your code, you will get the value of the corresponding posts field.
With the $variable $post_id in the CRED form you will always access the current edited/created post ID.

Now your latest explanation is completely different from what I understand from your initial request.

" I want to be able to do that automatically when a user submits a post with a certain city value which would initially be a Types custom field. It's that field that I want to be appending in the medafi_city dropdown values."

This is not appending a value, but changing the value of your 3rd party Custom Field according the value the User passes in your Types Custom Field.

This is done by the same process in a CRED API code, you will get_post_meta() of your Types Field, and then use update_post_meta with the value coming from the Types Field, if those stored Values are exactly identical.

You can use also if{} conditionals in PHP, so you can tell "if the types value is [value], then save my 3rd party Custom Field with this [value]"
hidden link

It seems -unfortunately- that for now you need custom programming work which is beyond the scope of our support.

At this point I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

I will be happy to assist CRED API and Toolset related issues, also with small snippets of code examples, which I provided above.

§Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

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