Tell us what you are trying to do?
I am trying to display my custom toolset-meta type "fictionlevel" in my REST API custom endpoint.
I want to do this:
$posts = get_posts($args);
$data = [];
$i = 0;
foreach ($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['title'] = $post->post_title;
return $data;
... but with my custom field 'fiction-level'.
something like:
$data[$i]['toolset'] = $post→ toolset-meta→fictionlevel→raw value
how do I address that correctly?
you can see the json output in the screenshot below.
Thank you!
Hi, we have some information about including Types custom field values in REST API responses available here: https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/
There is a setting in Toolset > Settings > Custom Content that will expose all Types fields to the REST API, or PHP filters you can use to expose specific fields as needed. The structure of the data model is slightly different from what you described, but should be sufficient to get the information you need. Let me know if you have problems implementing this and I can take a closer look.
Hi Christian,
thanks for the fast reply.
I just noticed that my $posts = get_posts($args) request does not give me my the custom fields at all in my new route qz/v1/, but I'm getting them in /wp/v2...
all the settings in toolset are on.
all parameters am getting are:
post_title:
post_excerpt:
post_status:
comment_status:
ping_status:
post_password:
post_name:
to_ping:
pinged: "",
post_modified:
post_modified_gmt:
post_content_filtered:
post_parent:
guid:
menu_order:
post_type:
post_mime_type:
comment_count:
filter:
any idea on how to make them appear here?
Thanks,
Albert
I just noticed that my $posts = get_posts($args) request does not give me my the custom fields at all in my new route qz/v1/
Your custom route just returns the response of get_posts? By default the get_posts API does not return custom field values in its responses, as each item is a WP_Post object https://developer.wordpress.org/reference/classes/wp_post/. That class does not expose postmeta fields directly, so I would not expect to have access to them if you're just returning the results of the get_posts query. You'll probably have to call get_post_meta in your callback to fetch individual field values for each post, then insert them in the response somehow. You can register individual fields in the response using the register_api_field function as shown in this post: https://wordpress.stackexchange.com/questions/223219/how-do-i-add-user-custom-field-to-rest-api-response
The rest field registration and implementation part of the process isn't really related to Toolset, so it's not something we support here. However, I can help you retrieve Types field values from the database if you need help with that. When using WP APIs like get_post_meta, you must use the full Types field slug stored as in the database, with wpcf- prefix, e.g.:
$field_value = get_post_meta( $post_id, 'wpcf-your-field-slug', true);
Thanks! The hint with the get_post_meta function gave me the right direction.