Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Advanced Custom Fields Fields to the WP REST API
function wp_api_encode_acf($data,$post,$context){
$data['meta'] = array_merge($data['meta'],get_fields($post['ID']));
return $data;
}
if( function_exists('get_fields') ){
add_filter('json_prepare_post', 'wp_api_encode_acf', 10, 3);
}
@adriancbo

This comment has been minimized.

Copy link

adriancbo commented Sep 1, 2014

Awesome!

@jaredshaunsmith

This comment has been minimized.

Copy link

jaredshaunsmith commented Sep 3, 2014

I had to typecast the get_fields() returned value as an array. updated code below:

function wp_api_encode_acf($data,$post,$context){
    $customMeta = (array) get_fields($post['ID']);

    $data['meta'] = array_merge($data['meta'], $customMeta );
    return $data;
}

if( function_exists('get_fields') ){
    add_filter('json_prepare_post', 'wp_api_encode_acf', 10, 3);
}
@chiastic-security

This comment has been minimized.

Copy link

chiastic-security commented Sep 4, 2014

Does this work in both directions? Can I push updates to the meta to change the ACFs programmatically?

@Schurke

This comment has been minimized.

Copy link

Schurke commented Sep 11, 2014

This made my day. Well done!

@pimoGit

This comment has been minimized.

Copy link

pimoGit commented Sep 15, 2014

This made my day too! I love you! :-)

@paublyrne

This comment has been minimized.

Copy link

paublyrne commented Oct 16, 2014

Searched two hours before I found this. You're a legend! Thanks

@PanMan

This comment has been minimized.

Copy link

PanMan commented Oct 23, 2014

Cool, this works! I created a plugin from this, as I prefered that over editing the main plugin. It is to be found at https://github.com/PanManAms/WP-JSON-API-ACF

@paublyrne

This comment has been minimized.

Copy link

paublyrne commented Nov 6, 2014

I was having a problem with this when I tried to make a call to get a post without custom fields, meta data was being overwritten, so I changed the function to check first. Seems obvious, but was tearing my hair out over this for a while trying to figure out what was going wrong, so reposting in case it can help someone else.

function wp_api_encode_acf($data,$post,$context){
// check if custom fields exist for the post
$custom_fields_exist = get_fields($post['ID']);
if ($custom_fields_exist) {
$data['meta'] = array_merge($data['meta'], get_fields($post['ID']));
}
return $data;
}

@kokarn

This comment has been minimized.

Copy link

kokarn commented Nov 21, 2014

Please note, because you do a check for get_fields in the code this will fail if this code loads before ACF

@lapidus

This comment has been minimized.

Copy link

lapidus commented Dec 1, 2014

Can one also filter by these fields? posts/?filter[meta_key]=something

@gino8080

This comment has been minimized.

Copy link

gino8080 commented Mar 7, 2015

Great!
can I also EDIT acf metas ?
can you show me an example please?

@t0t

This comment has been minimized.

Copy link

t0t commented Apr 30, 2015

it would be fantastic filter by meta the custom fields!

@kilianheinrich

This comment has been minimized.

Copy link

kilianheinrich commented May 22, 2015

Great!

@lukdanek

This comment has been minimized.

Copy link

lukdanek commented Jul 17, 2015

Hi there, any chance to get this done for taxonomies?

@snstarosciak

This comment has been minimized.

Copy link

snstarosciak commented Oct 3, 2015

Hey I'm just now stumbling on this thread and I actually have added this plugin which does work, for the most part: https://wordpress.org/plugins/acf-to-wp-api/

The problem is that I have a custom taxonomy that's setup like an archive post, but it still has a bunch of ACF fields associated with it. When I view the posts in my browser, I get a "acf: false" key mapping for the custom post type, but for regular standard Wordpress posts, the ACF fields show up in the json api object. Does anyone have any clue why this would be happening? Am I missing something?

@florianboudot

This comment has been minimized.

Copy link

florianboudot commented Oct 16, 2015

Hello, how do you use it ?
I tried : http://localhost/wp-json/wp/v2/posts/16/to retrieve all the post infos and but the meta section goes :

http://v2.wp-api.org/meta: [
    {
        embeddable: true,
        href: "http://localhost/wp-json/wp/v2/posts/16/meta"
    }
]

I can't see my custom fields, and when I try this link it returns 403 has I'm not logged, but I want this data to be public read only.
Thanks

@davidmaneuver

This comment has been minimized.

Copy link

davidmaneuver commented Nov 16, 2015

I use the following when using v2 of https://github.com/WP-API/WP-API

function wp_rest_api_alter() {
  register_api_field( 'post',
      'fields',
      array(
        'get_callback'    => function($data, $field, $request, $type){
          if (function_exists('get_fields')) {
            return get_fields($data['id']);
          }
          return [];
        },
        'update_callback' => null,
        'schema'          => null,
      )
  );
}
add_action( 'rest_api_init', 'wp_rest_api_alter');
@brospars

This comment has been minimized.

Copy link

brospars commented Feb 8, 2016

@davidmaneuver Thank you so much !

@thomers

This comment has been minimized.

Copy link

thomers commented Feb 23, 2016

This is working with REST API v2 for me: https://github.com/airesvsg/acf-to-rest-api

@Shobby

This comment has been minimized.

Copy link

Shobby commented Feb 23, 2017

I have created a custom post type and it has meta too. the custom post name is before_after and it is using multi post thumbnail library two metas are before_image and after_image, how i can add these two metas to REST API using the following code:

    function wp_api_encode_acf($data,$post,$context){

    $field_name = "after_image";
$data['mymeta'] = get_post_meta( $object[ 'id' ], $field_name );

return $data;
    }
    add_filter('json_prepare_post', 'wp_api_encode_acf', 10, 3);
@heidipowers

This comment has been minimized.

Copy link

heidipowers commented Oct 26, 2017

Thank you!

@mehmoodak

This comment has been minimized.

Copy link

mehmoodak commented Aug 20, 2019

The given solution didn't work for me on WordPress 5.2 but this works.

/**
 * Create REST field of ACF values for WordPress REST API
 */
function create_acf_fields_key_for_rest_api() {
	register_rest_field(
		[ 'post', 'page' ], // add post types here
		'acf_fields', // name of the field
		array(
			'get_callback'    => 'get_acf_fields_for_api',
			'update_callback' => null,
			'schema'          => null,
		)
	);
}
add_action( 'rest_api_init', 'create_acf_fields_key_for_rest_api' );

/**
 * Get ACF fields of the current post for the REST API field
 *
 * @param array $object post object array
 * @return array acf fields related to current post
 */
function get_acf_fields_for_api( $object ) {
	$data = get_fields( get_the_ID() );
	return $data ?: false;
}
@ThomasDK81

This comment has been minimized.

Copy link

ThomasDK81 commented Aug 26, 2019

@mehmoodak: Thank you :)

@albertoavv

This comment has been minimized.

Copy link

albertoavv commented Nov 3, 2019

I am trying to send a post to my post type and it gives me an error, I am using custom field, help please, thank you
image

@mehmoodak

This comment has been minimized.

Copy link

mehmoodak commented Nov 3, 2019

@albertoavv The REST route you are giving isn't correct. I didn't work with ACF REST API so please read their docs carefully. By the way I also thinks that in your path wp/wp5/wp-json should be changed to wp/wp-json

@albertoavv

This comment has been minimized.

Copy link

albertoavv commented Nov 3, 2019

Thank you for your response, if what happens is that w5 is the name of the project or the root, but thanks for your response I will continue looking for a solution,

Regards

@DonDi1989

This comment has been minimized.

Copy link

DonDi1989 commented Jan 20, 2020

@mehmoodak thnx for your solution. Do you have any suggestion to specify only which ACF fields do you want to retrieve or not ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.