Skip Navigation

[Resolved] Extracting multiple instances of audio field in REST api

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 7 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#456525

I am trying to: The goal is to take advantage of the "multiple instances" option in the toolset post fields area. I selected the type as audio, this has a multiple instances allowance option selected. I would like to have multiple mp3 files attached to my posts and have them show up in the REST Api Version 2 endpoint. I can get the individual fields to show with the .mp3 file on it but I was unable to get all of the files to show in the endpoint.

I visited this URL: hidden link

I expected to see: wpcf-firstbead with an array or object - not sure how the fields are stored in toolset's "multiple instances" option.

Instead, I got: A single link to the mp3 file.

I got this in my functions.php file:

add_action('rest_api_init', 'register_custom_fields'); 

function register_custom_fields(){
  register_rest_field(
    'rosary_prayers',
    'wpcf-firstbead',
    array(
      'get_callback' => 'show_fields'
      )
    );
}


function show_fields($object, $field_name, $request){
  return get_post_meta($object['id'], $field_name, true);

Thanks in advance for any help or input!

#456699

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I don't have any experience with the REST API myself, but I think I may have spotted your problem.

Types repeating fields simply adds multiple instances of the custom field to the wp_postmeta table, i.e. it creates multiple occurrences with the same meta_key.

get_post_meta takes 3 arguments, the last of which is a boolean to specify whether to return a single value, and you have this set to true. Setting it to false will return an array of all instances instead of a string of the first one.

The forum ‘Types Community Support’ is closed to new topics and replies.

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