1

I used Custom Field Suite to add fields to all WordPress posts, but the field data does not show when I retrieve post data through the WP Rest API using /posts on a stand alone site.

Is there a way to add the CFS data so that it is retrieved by wp-json/posts REST calls? It feels like I just need to put a CFS()->get call in the right place. Or just a basic way to add data to posts so they are retrievable in REST calls?

I've added github issues for CFS and WP-API, and a general Stack Overflow question but any help or direction would be appreciated. Thanks.

1

The WP API has hooks, like

rest_prepare_page

to enhance the endpoints. For each post type give it different hooks the follow code example add the tags to post and page and illustrate, how it works.

 /** 
   * Add properties to posts and pages endpoints.
   */function wp_api_theming_posts_properties( $response ) {    

    // Set author's name.   
    $author = get_userdata( $response->data['author'] );
    $response->data['author'] = array(      'id' => $author->ID,        'link' => get_author_posts_url( $author->ID ),      'name' => $author->data->display_name,  );  

     // Add post classes.
        $response->data['post_class'] = get_post_class( '', $response->data['id'] ); 

    // Add categories.  
   $categories = wp_api_theming_get_post_terms( $response->data['id'], 'category' );

    if ( ! empty( $categories ) ) { 
        $response->data['categories'] = $categories;
    } 

    // Add tags.    $tags = wp_api_theming_get_post_terms( $response->data, 'post_tag' );

    if ( ! empty( $tags ) ) {   
        $response->data['tags'] = $tags;    }   return $response;
   }

add_filter( 'rest_prepare_post', 'wp_api_theming_posts_properties' );

add_filter( 'rest_prepare_page', 'wp_api_theming_posts_properties' );  

/** * Get a post's terms with archive links. */
function wp_api_theming_get_post_terms( $id = false, $taxonomy = 'category' ) {

   // We need an ID for this one.
  if ( ! $id ) {        
      return FALSE; 
  }     

  // Validate the taxonomy argument.      
  $valid_tax = apply_filters( 'wp_api_theming_valid_tax', array( 'category', 'post_tag' ) );

  $taxonomy = ( in_array( $taxonomy, $valid_tax ) ) ? $taxonomy : 'category';   
  // Fetch our terms.   
 $terms = wp_get_post_terms( absint( $id ), $taxonomy );    

    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
       // Append a link property to each term.
        foreach ( $terms as $term ) {
               $link = get_term_link( $term );
               $term->link = $link;     
        }   
    }   

 return $terms;
}

Also see my small plugin to reduce the result of the API for different points. I think it also helpful to understand how you can change, enhance the result, the endpoints. https://github.com/bueltge/WP-REST-API-Filter-Items

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.