I am getting JSON data of toolset custom fields. the only issue is one of the fields is multiple instance and saved like following image in DB.
I need to extract this as array in json or if it's possible to save such field as array in the DB?
Or maybe it's possible to get all values and save them in a new hidden field?
Hi,
Thank you for contacting us and I'd be happy to assist.
The structure of storing multiple instances of the same custom field, as separate records in the database, is WordPress' standard way and I'm afraid, it can't be changed.
But if you could share some background information around how you're getting the JSON data and what are operations that you'll be performing on that, I'll be in a better position to guide you accordingly.
Note: If you're using custom PHP code to get that data, then you can use Toolset Fields API to get those custom field values in practically any desired format:
https://toolset.com/documentation/customizing-sites-using-php/functions/
regards,
Waqar
Yes i am using php to send metadata of a new product to a third party framework.
I managed to get all the fields but as said only the first value for multi instance field called 'wpcf-google-calendar-id'
// Woocommerce Product Action Triggers
add_action('cred_submit_complete', 'success_for_form_wc',10,2);
function success_for_form_wc($post_id, $form_data)
{
global $post;
//$event = null;
$meta = get_post_meta($post_id);
$title = get_the_title( $post_id );
$multiField = (types_render_field($post_id, 'wpcf-google-calendar-id', array( 'arg1' => 'val1', 'arg2' => 'val2' ) ));
// if its new product form
if ($form_data['id']==222) {
$response = wp_remote_post('<em><u>hidden link</u></em>', array(
'method' => 'POST',
'body' => array(
'ID' => $post_id,
'Title' => $title,
'CalID' => $multiField,
'Meta' => $meta,
)
));
}
}
I need to get the all the meta values in the variable $multiField of the field 'wpcf-google-calendar-id'.. not sure how to do this..
Thanks for writing back and for sharing further details.
To get the values of multiple instances of the field, you can update the line # 10 in your code snippet to:
$multiField = types_render_field( "google-calendar-id", array( 'item' => $post_id, 'separator' => ',' ) );
This will give you a comma-separated string of values from field with slug "google-calendar-id".
If you need to further convert this string into an array, PHP's "explode" function can be used.
hidden link
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
I have added following but not working! tried to add the wpcf- to the slug of the field i was thinking it's missing but also not showing array...
add_action('cred_submit_complete', 'success_for_form_wc',10,2);
function success_for_form_wc($post_id, $form_data)
{
global $post;
$meta = get_post_meta($post_id);
$title = get_the_title( $post_id );
$multiField = types_render_field( "wpcf-google-calendar-id", array( 'item' => $post_id, 'separator' => ',' ) );
if ($form_data['id']==222) {
$response = wp_remote_post('<em><u>hidden link</u></em>', array(
'method' => 'POST',
'body' => array(
'ID' => $post_id,
'Title' => $title,
'Multi' => $multiField,
'Meta' => $meta,
)
));
}
}
}
My issue is resolved now. Thank you! i didn't notice this is not the field i need to extract.
final code
add_action('cred_submit_complete', 'success_for_form_wc',10,2);
function success_for_form_wc($post_id, $form_data)
{
global $post;
$meta = get_post_meta($post_id);
$title = get_the_title( $post_id );
$multiField = types_render_field( "prize-value", array( 'item' => $post_id, 'separator' => ',' ) );
if ($form_data['id']==222) {
$response = wp_remote_post('<em><u>hidden link</u></em>', array(
'method' => 'POST',
'body' => array(
'ID' => $post_id,
'Title' => $title,
'Multi' => $multiField,
'Meta' => $meta,
)
));
}
}
}