Skip to content

Instantly share code, notes, and snippets.

@annelyse
Created June 18, 2021 15:01
Show Gist options
  • Save annelyse/a513f2be23cc46b6a6fc812337d44dd9 to your computer and use it in GitHub Desktop.
Save annelyse/a513f2be23cc46b6a6fc812337d44dd9 to your computer and use it in GitHub Desktop.
<?php
//Custom acf endpoint;
function add_acf_field_rest( $request_data ) {
// setup query argument
$args = array(
'post_type' => 'cpt_territory',
'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);
}
return $posts;
}
// register the endpoint;
add_action( 'rest_api_init', function () {
register_rest_route( 'my_endpoint/v2', '/cpt_territory/', array(
'methods' => 'GET',
'callback' => 'add_acf_field_rest',
)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment