Weird behavior when posting a media

I am currently experiencing an issue when creating media in WordPress using the REST API and Laravel HTTP Client. The media is being successfully uploaded, but I am unable to use it as a featured media until I manually edit and save it within the CMS. Here is a screenshot of how the media appears when uploaded using the API: (https://i.sstatic.net/fzmOeV76.png) I'm using this function : public function addMedia($entry) { $parsedUrl = parse_url($entry['image']); $url = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path']; try { $response = Http::withBasicAuth($this->username, $this->key) ->withHeaders([ 'Cache-Control' => 'no-cache', ]) ->attach( 'file', file_get_contents($url), basename($url) ) ->post($this->siteUrl . '/wp-json/wp/v2/media', [ 'title' => $entry['title'], 'caption' => $entry['title'], 'alt_text' => $entry['title'], 'description' => $entry['title'], 'status' => 'publish', 'author' => 1, ]); if ($response->successful()) { $responseData = $response->json(); Log::info('Media created in WP: ' . $responseData['id']); return $responseData['id']; } else { Log::error('Failed to create media in WP: ' . $response->body()); return false; } } catch (\Exception $e) { Log::error('Exception when creating media in WP: ' . $e->getMessage()); return false; } } and here's the response : array:30 [ "id" => 15021 "date" => "2024-09-15T10:15:52" "date_gmt" => "2024-09-15T08:15:52" "guid" => array:2 [ "rendered" => "https://xxx/wp-content/uploads/2024/09/919c5694-4eaf-463f-a600-a8f4fe9e85aa-13.jpg" "raw" => "https://xxx/wp-content/uploads/2024/09/919c5694-4eaf-463f-a600-a8f4fe9e85aa-13.jpg" ] "modified" => "2024-09-15T10:15:52" "modified_gmt" => "2024-09-15T08:15:52" "slug" => "test-2" "status" => "inherit" "type" => "attachment" "link" => "https://xxx/test-2/" "title" => array:2 [ "raw" => "Test" "rendered" => "Test" ] "author" => 1 "featured_media" => 0 "comment_status" => "" "ping_status" => "closed" "template" => "" "meta" => array:2 [ "_lmt_disableupdate" => "" "_lmt_disable" => "" ] "permalink_template" => "https://xxx/?attachment_id=15021" "generated_slug" => "test-2" "class_list" => array:5 [ 0 => "post-15021" 1 => "attachment" 2 => "type-attachment" 3 => "status-inherit" 4 => "hentry" ] "description" => array:2 [ "raw" => "Test" "rendered" => """ <p class="attachment"><a href='xxx/wp-content/uploads/2024/09/919c5694-4eaf-463f-a600-a8f4fe9e85aa-13.jpg'>Test</a></p>\n <p>Test</p>\n """ ] "caption" => array:2 [ "raw" => "Test" "rendered" => "<p>Test</p>\n" ] "alt_text" => "" "media_type" => "file" "mime_type" => "image/jpeg" "media_details" => [] "post" => null "source_url" => "xxx/wp-content/uploads/2024/09/919c5694-4eaf-463f-a600-a8f4fe9e85aa-13.jpg" "missing_image_sizes" => [] "curies" => array:1 [ 0 => array:3 [ "name" => "wp" "href" => "https://api.w.org/{rel}" "templated" => true ] ] ] ] The code has been working for several months, but I am currently experiencing errors. It is possible that the issue is related to a recent update to WordPress or to changes in the REST API. I have attempted to set the attachment type as an image by using the 'type' value of 'image' in the request, but it appears that this is not working as expected. Additionally, I have set the status to 'publish', but the returned value is 'inherit'.

Comment (0)

You’ll be in good company