Use (custom) fields from WP API

Hey,
how is it possible to use additional fields from WP API? For now, it seems that it’s filtered and I have to adapt wp-source to set additional fields in the models, is that correct or is there an easier option, maybe with adapting a configuration?

Thank you!

You just need to expose the custom fields in the REST API.

For ACF, that’s made with a plugin: https://wordpress.org/plugins/acf-to-rest-api/

For other custom fields, you’d need to either add them yourself or use a plugin like https://wordpress.org/plugins/wp-rest-api-controller/

One they are in the REST API response, you’ll be able to access them in Frontity, just like you access the title or content .

2 Likes

ok, that`s cool, but how ca i get the custom fields if it is custom fields in Theme options ?

i created it by these funcs. in functions.php

use Carbon_Fields\Container;

use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );

function crb_attach_theme_options() {

    Container::make( 'theme_options', __( 'Theme Options', 'crb' ) )

        ->add_fields( array(

            Field::make( 'text', 'crb_text', 'Text Field' ),

        ) );

}

how can i get the fields ?

Hi @jwd.05.mail

The documentation for Carbon Fields says the following about adding fields to the WP REST API:

Fields can be exposed in WordPress’ REST API by using their set_visible_in_rest_api() method:
Field::make(...)->set_visible_in_rest_api( $visible = true )

Hope this helps.

I added the ACF and ACF TO REST API plugin to my WP, however I see the empty array of acf in the wp-json. Any suggestion?

If the problem is in the REST API output maybe you are going to have more luck if you ask about it to the owner/maintainers of the ACF TO REST API plugin. I’m sure they’ll know more about the issue :slightly_smiling_face:

@mh123 Hi
Can you please try GitHub - airesvsg/acf-to-rest-api: Exposes Advanced Custom Fields Endpoints in the WordPress REST API
So you can get acf value, Please let me know you got a response.