Created
          June 18, 2021 15:01 
        
      - 
      
- 
        Save annelyse/a513f2be23cc46b6a6fc812337d44dd9 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?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