1

when I make a request to http://localhost/wordpress/wp-json/wp/v2/posts/?_embed&categories=${categoryId}&per_page=${state.perPage}

I get the following result

[                                                                                                            
  {
    id: 3,
    date: '2019-04-20T16:10:00',
    date_gmt: '2019-04-20T16:10:00',
    guid: {
      rendered: 'http://localhost/wordpress/?p=3'
    },
    modified: '2019-04-20T16:36:30',
    modified_gmt: '2019-04-20T16:36:30',
    slug: 'slug-title',
    status: 'publish',
    type: 'post',
    link: 'http://localhost/wordpress/2019/04/20/slug-title/',
    title: {
      rendered: 'title'
    },
    content: {
      rendered: '\n<figure class="wp-block-image"><img ' +
        'src="http://localhost/wordpress/wp-content/uploads/2019/04/sirius.jpg" ' +
        'alt="" class="wp-image-4" ' +
        'srcset="http://localhost/wordpress/wp-content/uploads/2019/04/sirius.jpg ' +
        '370w, ' +
        'http://localhost/wordpress/wp-content/uploads/2019/04/sirius-217x300.jpg ' +
        '217w" sizes="(max-width: 370px) 100vw, 370px" ' +
        '/></figure>\n',
      protected: false
    },
    excerpt: {
      rendered: '',
      protected: false
    },
    author: 1,
    featured_media: 0,
    comment_status: 'open',
    ping_status: 'open',
    sticky: false,
    template: '',
    format: 'standard',
    meta: [],
    categories: [
      3
    ],
    tags: [],
    _links: {
      self: [Array],
      collection: [Array],
      about: [Array],
      author: [Array],
      replies: [Array],
      'version-history': [Array],
      'predecessor-version': [Array],
      'wp:attachment': [Array],
      'wp:term': [Array],
      curies: [Array]
    },
    _embedded: {
      author: [Array],
      'wp:term': [Array]
    }
  }
]

But as you can see there is no featured_media. Do I have a misunderstanding about featured_media? I made my post with an image like you can see in content.rendered

4
  • 1
    Well, I don't think the post actually has a featured image. The one in the content.rendered is an image added using the Image block. You set the featured image via the "Set featured image" link in the "Featured Image" meta box, even when using the block editor.
    – Sally CJ
    Apr 25, 2019 at 16:12
  • 1
    Possible duplicate of WP Rest API - How to get featured image
    – leymannx
    Apr 25, 2019 at 16:13
  • @evgenifotia, I'm glad the comment helped. And just to be clear, this is what I meant, which is for the block editor. :)
    – Sally CJ
    Apr 25, 2019 at 16:30
  • 1
    @SallyCJ I have wordpress in french that's why I haven't realized that meta box thank you Apr 25, 2019 at 16:39

1 Answer 1

0

You don't set featured image for your post ,so return 0 in feature image obj. after set it you can use featured image obj.

for example: I wrote this with PHP, you can upload this to another host and display the images of the WordPress website that you specified in the URL variable on those hosts.

$url = 'url of your wordpress website';
function Json_Data_Decoder($query){     
    global $url;
    $json = file_get_contents($url.'/wp-json/wp/v2/'. $query);
    $data = json_decode($json);
    return $data;
}
function Get_Media($post_media){
    global $url;
    $json = file_get_contents($url.'/wp-json/wp/v2/media?include='.$post_media);
    $medias = json_decode($json);
    $featured_image = $medias[0]->media_details->sizes->medium->source_url;
    return $featured_image;
}

$posts = Json_Data_Decoder('posts');
foreach ($posts as $post){
    
    // get media 
    $post_media = $post->featured_media;
    $featured_image = Get_media($post_media);
    
    echo 
    '<article class="post image">
            <img src="'.
                $featured_image .
            '">
    </article>';
}
2
  • In WordPress's REST API, the featured_media should be automatically set, without needing a plugin. It'll return the featured image's ID or 0 if there's not a featured image.
    – Pat J
    Nov 17, 2020 at 1:59
  • you're right. I edited this answer. Nov 17, 2020 at 15:01

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.