how do i add an array of objects or an extra field to a wordpress json api core controller?

Is there a way to add an array of objects to a core controller of the json apiplugin

the core controller i want to modify is ‘get_category_index’ and it returns these objects

Read More
  1. id
  2. post_count
  3. name
  4. slug

what i want to do is an extra object, ‘posts’ which lists the posts that are assigned to that category

the code currently looks like this in jsonapi/controllers/core.php for that specific controller looks like that

public function get_category_index() {
    global $json_api;
    $args = null;
    if (!empty($json_api->query->parent)) {
      $args = array(
        'parent' => $json_api->query->parent
      );
    }
    $categories = $json_api->introspector->get_categories($args);
    return array(
  'count' => count($categories),
  'categories' => $categories
    );
  }

and i thought something like this would do the trick

public function get_category_index() {
    global $json_api;
    $args = null;
    if (!empty($json_api->query->parent)) {
      $args = array(
        'parent' => $json_api->query->parent
      );
    }
    $categories = $json_api->introspector->get_categories($args);
    return array(
  'count' => count($categories),
  'categories' => $categories,
  'posts' => $posts,
    );
  }

but i’m obviously im missing something, how can i add that array of posts?

i found the category.php which looks like this

class JSON_API_Category {

  var $id;          // Integer
  var $slug;        // String
  var $title;       // String
  var $description; // String
  var $parent;      // Integer
  var $post_count;  // Integer
  var $posts;  // array

  function JSON_API_Category($wp_category = null) {
    if ($wp_category) {
      $this->import_wp_object($wp_category);
    }
  }

  function import_wp_object($wp_category) {
    $this->id = (int) $wp_category->term_id;
    $this->slug = $wp_category->slug;
    $this->title = $wp_category->name;
    $this->description = $wp_category->description;
    $this->parent = (int) $wp_category->parent;
    $this->post_count = (int) $wp_category->count;
    $this->posts = $wp_category->posts;
  }

}

where i added the

var $posts;  // array

and called it

$this->posts = $wp_category->posts;

however obviously i’m getting null on posts in my json return, whereas i need it to return the posts in that category.

Related posts

Leave a Reply