WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#44219 closed defect (bug) (invalid)

null post json object reference when retrieve a media item

Reported by: nurcholisart Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8.2
Component: REST API Keywords:
Focuses: rest-api Cc:

Description

hello, i have problem with my wp json api, and if i'm not wrong, maybe this is a bug in wp json api. So, when i try to retrieve a post item, the json object show that the post has featured media by id 7636.

{
	// retrieved post item
	
	"id": 7634,
	"date": "2018-04-16T09:56:54",
	"date_gmt": "2018-04-16T02:56:54",
	
	...
	
	"author": 79,
	"featured_media": 7636,
	"comment_status": "open",
	"ping_status": "open",
	"sticky": false,
	"template": "",
	"format": "standard",
	"meta": [],
	
	....
}

full response here: https://himmahonline.id/wp-json/wp/v2/posts/7634

But, when i try to retrieve media with id 7636, the "post" json object is null. why this is happen? thanks before.

{
	// retrieved media item
	
	"id": 7636,
	"date": "2018-04-15T17:20:46",
	"date_gmt": "2018-04-15T10:20:46",
	
	...
	
	"post": null,
	"source_url": "https://himmahonline.id/wp-content/uploads/2018/04/DSC_0384.jpg",
	
	...
}

full response here: https://himmahonline.id/wp-json/wp/v2/media/7636

Change History (6)

#1 follow-up: @birgire
3 years ago

  • Keywords reporter-feedback added

Welcome to WordPress Trac @nurcholisart

I think that means the featured image is not attached to any post, hence null.

An image can only be attached to a single post.

So I think this is expected, if your featured image is not attached to the post.

Is that the case?

#2 @birgire
3 years ago

ps: by attached, I mean uploaded to the post, that sets the post_parent field for the image in the wp_posts table.

#3 in reply to: ↑ 1 @nurcholisart
3 years ago

Replying to birgire:

Welcome to WordPress Trac @nurcholisart

I think that means the featured image is not attached to any post, hence null.

An image can only be attached to a single post.

So I think this is expected, if your featured image is not attached to the post.

Is that the case?

Hi, @birgire thanks before...

Actually, i have uploaded the image to that post in my wordpress dashboard. and the media is just for that post. here is my screen shot http://oi64.tinypic.com/4r6fqt.jpg.

But when i get it from wp rest api, the "post" object give me null return. i think it should return "post":7634. because, when i try to get other image detail from wp rest api, it's not give null return. example is like this:

{
	"id": 7839,
	"date": "2018-05-24T16:03:24",
	"date_gmt": "2018-05-24T09:03:24",
	
	...
	
	"author": 93,
	"featured_media": 7846,
	
	...
}

the code above is response when i get https://himmahonline.id/wp-json/wp/v2/media/7846. and that image is the featured media in post id 7839. here is the response when i retrieve post detail:

{
	"id": 7839,
	"date": "2018-05-24T16:03:24",
	"date_gmt": "2018-05-24T09:03:24",
	
	....
	
	"author": 93,
	"featured_media": 7846,
	"comment_status": "open",
	
	...
}

https://himmahonline.id/wp-json/wp/v2/posts/7839

The featured image object appears in json response.

i need this to export my wordpress database to non-wordpress database.

#4 follow-up: @birgire
3 years ago

@nurcholisart

Just to make sure we're talking about the same thing, can you please try the following:

  1. Go to the edit post screen: /wp-admin/post.php?post=7634&action=edit.
  2. Click the "Add Media" button.
  3. Upload a new image, from your computer.
  4. Close the Media popup.
  5. Set that new image as the featured image.
  6. Save post.

and check again

  • /wp-json/wp/v2/posts/7634
  • /wp-json/wp/v2/media/???

Thanks.

ps: the featured image is stored in the post meta, so it doesn't have to be attached to the post.

#5 in reply to: ↑ 4 @nurcholisart
3 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Replying to birgire:

@nurcholisart

Just to make sure we're talking about the same thing, can you please try the following:

  1. Go to the edit post screen: /wp-admin/post.php?post=7634&action=edit.
  2. Click the "Add Media" button.
  3. Upload a new image, from your computer.
  4. Close the Media popup.
  5. Set that new image as the featured image.
  6. Save post.

and check again

  • /wp-json/wp/v2/posts/7634
  • /wp-json/wp/v2/media/???

Thanks.

ps: the featured image is stored in the post meta, so it doesn't have to be attached to the post.

hi @birgire i have tried your suggestion, and now it's work! now i can see the post object value in /wp-json/wp/v2/media/7636. Thank you very much...

#6 @birgire
3 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted

@nurcholisart glad to hear it worked for you.

Note: See TracTickets for help on using tickets.