First of all, I am talking about this plugin: https://wordpress.org/plugins/json-api/

So now regarding my problem: The plugin is working fine and without any problems. All attachments are showen for all posts, but my problem begins, when i want to get an attachment for post and the attachment was used earlier in another post. In this case it returns null (no attachement, or not the reused one).

So to be clear:

Post A
Image A  --> shows

Post B
Image A --> doesn't shows

So why this happens, and how to fix this? Thanks for any help!

  • Could you please elaborate on which code you are actually running? Attachments can only have one post parent, even if they are used in other ways with multiple posts. – Rarst Apr 28 '15 at 15:26
  • @Rarst no you miss understood me: i mean i don't get an image for a post if the image was used before in another post. – Deniz Celebi Apr 28 '15 at 20:52
  • how have you attached an image to multiple posts? attachments only have one parent. – Milo Apr 28 '15 at 20:59
  • @Milo in WordPress post editor you click on add media and select an uploaded image. This way you can add an image to as many posts as you want. This is nothing new? – Deniz Celebi Apr 28 '15 at 21:02
  • That doesn't attach an image to a post. Uploading an image while editing a post attaches it, selecting an already uploaded image doesn't. – Milo Apr 28 '15 at 21:05

So there are several ways images can be "related" to post:

  1. When you upload image in post context that post is set as its parent (that is parent field of image attachments post has post's ID).
  2. When you set image as a featured on a post (or use similar functionality from custom fields frameworks) the post records image attachment's ID in it its meta.
  3. When you insert image into post content, the details of image attachment's post are used to produce the markup.

Out of these the third one is the weakest to work with by code. If the only established link is the HTML markup with the URL to image then it is challenging to parse it out and reverse into meaningful ID. Not impossible, but it's a mess.

If you have very specific use case like single image associated with the post for special purpose, then you should be using featured images or similar approach to work with IDs.

  • Thanks for the answer, but can you maybe explain me why the plugin doesn't return the image in the json response? Would be very helpfull – Deniz Celebi Apr 29 '15 at 7:33
  • @DenizCelebi because if you have just inserted image into post content then there is nothing to return, other that that content. – Rarst Apr 29 '15 at 7:35
  • So how can i then reuse an image in a post, if it was used before in another post? Is there a way without reuploading it? – Deniz Celebi Apr 29 '15 at 7:37
  • @DenizCelebi as per answer — you should use featured images or analogous technique. – Rarst Apr 29 '15 at 7:47

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.