Skip Navigation

[Resolved] Extracting multiple instance Field

This support ticket is created 4 years 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.

Our next available supporter will start replying to tickets in about 2.23 hours from now. Thank you for your understanding.

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/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nashaat 3 years, 12 months ago.

Assisted by: Waqar.

Author
Posts
#1589067
1-field-3-instances.png

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?

#1589667

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#1589873

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

#1592729

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#1592759

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,
          	)
    	));
    }
  
    }
  	
}
#1593343

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,
            )
        ));
    }
   
    }
     
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.