WordPress.org

Forums

JSON API
[resolved] Remove unnecessary fields from json response (4 posts)

  1. Soulcyber
    Member
    Posted 1 year ago #

    I want retrieve the last posts using, get_recent_post

    http://mydomain.com/api/get_recent_posts/

    Everything works fine, but i want call this api from my Android application and i want minimize the data sent by the server.

    the response format is the following:

    "status":"ok",
    "count":5,
    "count_total":5,
    "pages":1,
    "posts":[]

    and each post has the following fields :

    "id":21,
    "type":"post",
    "slug":"articolo-prova-3",
    "url":"http://mydomain.com/index.php/2014/02/17/articolo-prova-3/",
    "status":"publish",
    "title":"Articolo prova 3",
    "title_plain":"Articolo prova 3",
    "content":"",
    "excerpt":"",
    "date":"2014-02-17 16:00:12",
    "modified":"2014-02-17 16:12:04",
    "categories":[],
    "tags":{},
    "author":{},
    "comments":{},
    "attachments":[],
    "comment_count":0,
    "comment_status":"open",
    "thumbnail":"http://mydomain.com/wp-content/uploads/2014/02/20100204104618Terminator-150x150.jpg",
    "custom_fields":{},
    "thumbnail_size":"thumbnail",
    "thumbnail_images":{}

    now i want lightweight the response removing some of this field/arrays

    i'm trying to modify the core api without success, i think it's easy but not enough for a PHP noob like me :D

    public function get_posts() {
        global $json_api;
        $url = parse_url($_SERVER['REQUEST_URI']);
        $defaults = array(
          'ignore_sticky_posts' => true
        );
        $query = wp_parse_args($url['query']);
        unset($query['json']);
        unset($query['post_status']);
        $query = array_merge($defaults, $query);
        $posts = $json_api->introspector->get_posts($query);
    
        foreach ($posts as $post) {
            foreach ($post as $field) {
                 unset($field['categories']); //i want remove this
                 unset($field['author']); //i want remove this
    	     unset($field['type']);  //i want remove this
    	}
    
        }
    
        $result = $this->posts_result($posts);
    
        $result['query'] = $query;
        return $result;
      }

    What i'm doing wrong?

    Thank you :)

    https://wordpress.org/plugins/json-api/

  2. monkeypunch3
    Member
    Posted 1 year ago #

  3. Soulcyber
    Member
    Posted 1 year ago #

    Yes but i want to exclude a lof of things (and the url could be too much longer with all the exclude arguments)

    I've solved editing get_recent_post in core php

    public function get_recent_posts() {
        global $json_api;
        $posts = $json_api->introspector->get_posts();
    	foreach($posts as $post){
    
    		$post->content = strip_tags($post->content, "<p>");
    
    		foreach ($post->attachments as $attachment){
    
                            unset($attachment->images['full']);
                            unset($attachment->images['medium']);
                            unset($attachment->images['large']);
                            unset($attachment->images['bones-thumb-300']);
                            unset($attachment->images['bones-thumb-600']);
                            unset($attachment->images['post-thumbnail']);
                            unset($attachment->images['vendor-thumb']);
                            unset($attachment->images['0']);
    			unset($attachment->images['et-featured-first']);
    			unset($attachment->images['et-featured-second']);
    			unset($attachment->images['et-featured-category']);
    			unset($attachment->images['et-featured-third']);
    			unset($attachment->images['et-recent-post-image']);
    			unset($attachment->images['et-popular-post-image']);
    			unset($attachment->images['et-popular-post-big-image']);
    			unset($attachment->images['et-category-image']);
    			unset($attachment->images['et-recent-first-image']);
    			unset($attachment->images['et-tabs-image-small']);
    			unset($attachment->images['et-blog-page-thumb']);
    			unset($attachment->images['et-gallery-page-thumb']);
    			unset($attachment->images['et-portfolio-medium-page-thumb']);
    			unset($attachment->images['et-portfolio-medium-portrait-page-thumb']);
    			unset($attachment->images['et-portfolio-small-page-thumb']);
    			unset($attachment->images['et-portfolio-small-portrait-page-thumb']);
    			unset($attachment->images['et-portfolio-large-page-thumb']);
    			unset($attachment->images['et-portfolio-large-portrait-page-thumb']);
            }
    
    		unset($post->thumbnail_size);
    		unset($post->thumbnail_images);
    		unset($post->custom_fields);
    		unset($post->slug);
    		unset($post->excerpt);
    	}
        return $this->posts_result($posts);
      }
  4. tobiWP
    Member
    Posted 1 year ago #

    Take this solution inside a custom controller:

    $query = array(
                'post_type' => 'objekt',
                'include' => 'title,author'
            );
    
            /*
             * WORKAROUND by nerevo
             */
            if ($query['include']) {
                $json_api->response->include_values = array_merge($json_api->response->include_values, explode(',', $query['include']));
            }
    
            $posts = $json_api->introspector->get_posts($query);

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • JSON API
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic