0

Not having access to the functions file, I can't add the function to add the featured image to the API so I need to use wp-json/wp/v2/posts?_embed.

With javascript I believe it is ._embedded['wp:featuredmedia']['0'].source_url so I tried with php after using wp_remote_get, $post->_embedded['wp:featuredmedia'][0]->source_url but the error I am getting is :

Cannot use object of type stdClass as array

4
  • 1
    Your question is a PHP question that's better suited at Stack Overflow. And I just wanted to say FYI, you're getting that error because $post->_embedded is an object which is an instance of the generic "empty" class in PHP, namely stdClass. So you should've instead used $post->_embedded->{'wp:featuredmedia'}.
    – Sally CJ
    Commented Jun 2, 2022 at 13:52
  • @SallyCJ good point, I just thought to ask here in the hope that someone else had this issue. Doing as you suggested was spot on and worked perfectly. You should add it as an answer :) Commented Jun 3, 2022 at 5:06
  • So I guessed it correctly that you used something like $post = json_decode( 'JSON string' ) ? 🙂 And I actually wondered - why do you need to use wp_remote_get() if the code runs on the same site where you're making the REST API request to? I mean, can't you just do a new WP_Query and get the featured image (URL) using WordPress functions?
    – Sally CJ
    Commented Jun 5, 2022 at 17:42
  • @SallyCJ I need to use wp_remote_get because I am loading posts from another website via the API, so I can't use WP_Query. Commented Jun 6, 2022 at 9:12

1 Answer 1

1

One approach is to cast the objects as arrays. I recently had to do this in a project where I retrieved a list of articles and needed to display the featured image. There may be alternate ways to handle this issue, but I've found it to be reliable. Here is some relevant code from the project.

<?php
  foreach ($articles as $index => $article) {
    $article = (array)($article);
    $embedded = (array)$article['_embedded'];
    $featuredMedia = (array) $embedded['wp:featuredmedia'][0];
    $mediaDetails = (array) $featuredMedia['media_details'];
    $sizes = (array) $mediaDetails['sizes'];
    $mediumLarge = (array) $sizes['medium_large'];

    $article_content = "
      <div class='col-xs-12 col-md-$count'>
        <a href='" . $article['link'] . "'>"
          . 
          "<img src='" . $mediumLarge['source_url'] .  "'/>".
          "<p>" . $article['title']->rendered . "</p>
        </a>
      </div>
      ";
    echo $article_content;
  }

2
  • 1
    Thanks for this answer, I used as suggested, $post->_embedded->{'wp:featuredmedia'} and that was enough to solve the issue. But it is good to know another method as you have provided. Commented Jun 3, 2022 at 5:07
  • @user8463989 glad you got it working :-) believe it or not, I wasn't aware of the {'string'} syntax. I'll likely change my project to match as well
    – aberkow
    Commented Jun 3, 2022 at 13:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.