WordPress Rest API Custom Endpoint displaying ACF Custom Fields

I’m trying to create a custom endpoint displaying custom post types, including custom field for those post types.

I have enabled these custom fields to be published in the REST API, by registering them:

register_rest_field( ‘pakketten’, ‘daily_edition’, array(
‘get_callback’ => function( $post ) {
return get_field(‘daily_edition’);
}
));

The endpoint for the custom endpoint ( /wp-json/wp/v2/pakketten ) shows the custom field just fine, but I can’t seem to find a way to get this data to be shown in a custom endpoint.

The custom endpoint is created as:

function cmcg_register_api_hooks() {
register_rest_route( ‘pakket-selector/v1’, ‘/pakketten/’, array(
‘methods’ => ‘GET’,
‘callback’ => ‘cmcg_get_all_fields’,
) );
}
add_action(‘rest_api_init’, ‘cmcg_register_api_hooks’);

function cmcg_get_all_fields($data) {
if (false === ($all_regions = get_transient(‘cmcg_all_regions’))) {
$all_regions = get_posts(array(
‘numberposts’ => -1,
‘post_type’ => ‘pakketten’,
‘fields’ => ‘region’
));
// cache
set_transient(‘cmcg_get_all_regions’, $all_regions, 1);
}
return $all_regions;
}

Here I am trying to get the custom field ‘region’ to be published on the endpoint /pakket-selector/v1/pakketten/

Am I on the right path to access the custom field data or completely off?

Read more here:: WordPress Rest API Custom Endpoint displaying ACF Custom Fields

Leave a Reply

Your email address will not be published. Required fields are marked *