Francisco
Francisco

Reputation: 35

Wordpress API REST format JSON, GET Featured Image with 1 call

I´m doing a APP in Android using the API REST to get data from my wordpress page. I want to see the image, the title and the description of the post in my app.

Wordpress return the media with this format:

"author": 5,
"featured_media": 1836,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"meta":

The GET service only give me the media id and I want the source url. I have to call to "PAGEURL/wp-json/wp/v2/media/IDMEDIA" to get the source url. This is too inefficient and too low. How could I get the source url with 1 call to GET service? I try to edit the plugin but it isn´t work.

This is the function to get the data from Wordpress

public class FeedTask extends AsyncTask<String, Void, ArrayList<JSONArray>>{

    private OnFeedListener listener;

    public FeedTask(OnFeedListener listener){
        this.listener=listener;
    }

    @Override
    protected ArrayList<JSONArray> doInBackground(String... params) {
        ArrayList<JSONArray> arrays=new ArrayList<>();
        String url=params[0];
        OkHttpClient client=new OkHttpClient();
        Request.Builder builder=new Request.Builder();
        Request request=builder.url(url).build();
        try {
            Response response=client.newCall(request).execute();
            String json=response.body().string();
           try{
               JSONArray array= new JSONArray(json);
               //Array de Imágenes
               JSONArray arrayImages=new JSONArray();
               for(int i=0; i<array.length(); i++) {
                   String urlImage=("http://PAGEURL/wp-json/wp/v2/media/")+array.optJSONObject(i).optString("featured_media");
                   request=builder.url(urlImage).build();
                   response=client.newCall(request).execute();
                   json=response.body().string();
                   arrayImages.put(new JSONObject(json));
               }
               arrays.add(array);
               arrays.add(arrayImages);
               return arrays;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(ArrayList<JSONArray> array) {
        super.onPostExecute(array);
        if(array==null){
            return;
        }
        if(listener!=null){
            listener.onFeed(array);
        }
    }
}

Upvotes: 0

Views: 2976

Answers (1)

Tosin Onikute
Tosin Onikute

Reputation: 4002

You can get Featured image with one call.

You shouldn't edit the plugin, your edits will be overridden on an update.

You should use the _embed parameter to allow featured image to show

http://example.com/wp-json/wp/v2/posts?&_embed=true

Data would be returned to you as _embedded , which contains an Object

"wp:featuredmedia" => [ "media_details" => sizes => { thumbnail, medium, full } ]

Upvotes: 5

Related Questions