Skip Navigation

[Resolved] Include custom fields in REST API PHP Query

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to include Types custom field values from my custom post type in REST API queries.

Solution: You can expose custom field values in standard post reponses in the REST API by turning on the REST setting in Toolset > Settings > Custom Content. In other custom API endpoints, you may need to register each custom field using the register_api_field API and get_post_meta (raw field values) or types_render_field API (formatted field values).

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/
https://toolset.com/documentation/customizing-sites-using-php/functions/

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by albertB-4 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1862767
Screenshot 2020-12-01 at 15.39.11.png

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!

#1862823

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.

#1862895

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

#1862939

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);
#1868661

Thanks! The hint with the get_post_meta function gave me the right direction.