I use http://github.com/jjgrainger/PostTypes/ to register my custom post types and ACF to add custom fields to my custom post and I want to include in my wp-json response all registered fields per post and I do the following

$options = [
    'supports' => array('revisions'),
    'has_archive' => false,
    'show_in_rest' => true,
    'rewrite' => array('slug' => __('teachers', 'teachers')),
    'capability_type' => 'post',
    'rest_base' => 'teachers',
    'query_var' => true,
    'rest_controller_class' => 'WP_REST_Posts_Controller',
$teachers = new PostType('teacher', $options);
$locations = new Taxonomy('location');
$levels = new Taxonomy('level');

$teachers->filters(['first_name', 'last_name']);
$teachers->columns()->hide(['title', 'date']);

    'first_name' => __('First Name'),
    'last_name' => __('Last Name'),


$teachers->columns()->populate('first_name', function ($column, $post_id) {
    echo get_post_meta($post_id, 'first_name')[0];

$teachers->columns()->populate('last_name', function ($column, $post_id) {
    echo get_post_meta($post_id, 'last_name')[0];

    'level' => __('Level'),


but in wp-json response I do not have any custom fields, than I tried after googling adding the following filter

function my_rest_prepare_post($data, $post, $request) {
    $_data = $data->data;

    $fields = get_fields($post->ID);
    foreach ($fields as $key => $value){
        $_data[$key] = get_field($key, $post->ID);
    $data->data = $_data;

    return $data;
add_filter("rest_prepare_teacher", 'my_rest_prepare_post', 10, 3);

but in this case I get a Fatal Error

Fatal error</b>:  Uncaught Error: Call to a member function get_links() on null in

How can I reflect in wp-json response on a custom post type all related custom field values?


I forgot to mention that on top of wordpress I use wordplate with sage and after tracking down rest_prepare_{$post_type} method I got to this point, which is working in my case

add_filter("rest_prepare_teacher", function($post) {
    $_data = $post->data;
    $fields = get_fields($_data['id']);

    foreach ($fields as $key => $value){
        $_data[$key] = get_field($key, $_data['id']);
    $post->data = $_data;
    return $post;
  • Are you using the ACF to REST API plugin? – WebElaine Sep 26 '18 at 21:35
  • No I do not use any kind of plug in except acf – fefe Sep 27 '18 at 4:47
  • Maybe you should use register_meta() with show_in_rest set to true. Reference – Sally CJ Sep 27 '18 at 5:36
  • I tried thanks, but without any success – fefe Sep 27 '18 at 7:05
  • Try the ACF to REST API plugin. It's built specifically to expose ACF fields in the REST API. – WebElaine Sep 27 '18 at 14:24

I really hope that you have managed this issue. In case you don't, this kinda works for me since there is a way to register a custom acf endpoint, in the functions.php file you have to add the next code:

//Custom acf endpoint;
function my_endpoint( $request_data ) {

    // setup query argument
    $args = array(
        'post_type' => 'my_post_type',
        'posts_per_page' => -1

    // get posts
    $posts = get_posts($args);

    // add custom field data to posts array 
    foreach ($posts as $key => $post) {
            $posts[$key]->acf = get_fields($post->ID);
            $posts[$key]->link = get_permalink($post->ID);
            $posts[$key]->image = get_the_post_thumbnail_url($post->ID);
    return $posts;

// register the endpoint;
add_action( 'rest_api_init', function () {
    register_rest_route( 'my_endpoint/v1', '/my_post_type/', array(
        'methods' => 'GET',
        'callback' => 'my_endpoint',

Credits to: https://clarencepearson.com/advanced-custom-fields-rest-api/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.