I made a custom endpoint with wp rest api for


which in turn runs the following wpdb query

$sql = "SELECT * 
FROM wp_posts p
WHERE p.post_type = 'location'
AND p.ID = 133;";
$results = $wpdb->get_results($sql);

Which returns fine but unfortunately my ACF fields aren't showing up in there. There are quite a few in the several queries I'm doing and this is going to turn into a mess quickly if I have to chase every field by hand. Is there an easier way to get posts with all the ACF fields attached?


I ended up using $wpdb for my custom tables and went with wp_query for my custom posts with ACF fields

    $q = new WP_Query(array(
        'post_type' => 'coupon',
        'post__in'  => $cids

    $coupons = array();
    while($q->have_posts()) : $q->the_post();
        $p = get_post();
        $f = get_fields();
        $m = array_merge((array)$p, (array)$f);
        array_push($coupons, $m);

And that's how the cookie crumbles.

function wpse187543_register_acf() {

    $post_types = get_post_types( arrray( 'public' => true ), 'names' );

    foreach ( $post_types as $type ) {

        register_api_field( $type, 'acf', array(
            'get_callback'    => 'wpse187543_get_acf',
            'update_callback' => null,
            'schema'          => null,


add_action( 'rest_api_init', 'wpse187543_register_acf' );

function wpse187543_get_acf( $object, $field_name, $request ) {
    return get_fields( $object['id'] );

This appends a key called “acf” to the rest api output for all public post types. You can customize the list of post types, of course. Also, there are some plugins that handle the job.

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.