Skip to content

REST API output does not include complex types #232

@ashervb

Description

@ashervb

What I expected

On the 3.2.0-beta1 branch, I would expect the API to return the serialized meta values for posts, arrays of posts and arrays created with the repeater field.

What happened instead

The meta values and repeater fields come back as "post". Example JSON:

[{
  "meta": {
      "home_testimonials": "post", // Relationship
      "home_hero_title": "Welcome", 
      "home_hero_image": {
          "width": 800,
          "height": 1000,
          "file": "2017\/05\/nfafn7r4.jpg",
          "sizes": {
              "thumbnail": {
                  "file": "nfafn7r4-150x150.jpg",
                  "width": 150,
                  "height": 150,
                  "mime-type": "image\/jpeg",
                  "url": "http:\/\/....\/wp-content\/uploads\/2017\/05\/nfafn7r4-150x150.jpg"
              },
              "medium": {
                  "file": "nfafn7r4-240x300.jpg",
                  "width": 240,
                  "height": 300,
                  "mime-type": "image\/jpeg",
                  "url": "http:\/\/....\/wp-content\/uploads\/2017\/05\/nfafn7r4-240x300.jpg"
              },
              "medium_large": {
                  "file": "nfafn7r4-768x960.jpg",
                  "width": 768,
                  "height": 960,
                  "mime-type": "image\/jpeg",
                  "url": "http:\/\/....\/wp-content\/uploads\/2017\/05\/nfafn7r4-768x960.jpg"
              }
          },
          "image_meta": {
              "aperture": "0",
              "credit": "",
              "camera": "",
              "caption": "",
              "created_timestamp": "0",
              "copyright": "",
              "focal_length": "0",
              "iso": "0",
              "shutter_speed": "0",
              "title": "",
              "orientation": "0",
              "keywords": []
          },
          "alt": "",
          "caption": "",
          "description": "",
          "id": 70,
          "is_image": true,
          "title": "nfafn7r4",
          "url": "http:\/\/....\/wp-content\/uploads\/2017\/05\/nfafn7r4.jpg"
      },
      "home_videos": "post", // Relationship
      "home_infographics": "post", // Repeater Field
      "home_tips": "post" // Relationship
  },
}]

If I create a hook for rest_api_init I can get the values to be output in the response:

add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
  register_rest_field( 'page', 'meta', array(
    'get_callback' => function ( $data ) {
      $meta = get_post_meta($data['id'], '', true);
      $ret = [];
      foreach ($meta as $key => $value) {
        if (substr($k,0,1) != "_") {
          $ret[$key] = papi_get_field($data['id'], $key);
        }
      }

      return $ret;
    },
  ));
}

However, this will not include the meta value of included posts.

Steps to reproduce

Create a custom page type that uses relationships or repeater fields

What versions of softwares are you using?

  • Browser: N/A
  • Papi: 3.2.0-beta1
  • WordPress: 4.7.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions