canbican/wordpress-java

Thumbnail Image upload correct but imposible to attach it to the post

Jesus90Martin opened this issue · 6 comments

Hi,

my question is simply, how can i attach an uploaded image to a post using for example post.setPost_thumbnail()?

I upload the image correctly but i cant attach it to the post, it will be awesome if you can show me a very simple example.

Thank you very much!! =)

How about getting the url of the thumbnail and inserting it to the post?

avoiding the usage of post.setPost_thumbnail() ?

setting it as a custom field is my last option, because it will force me to update wordpress theme code. Thats why i am looking for a better solution, and if finally there isnt one, i will have to update that code.

when i saw setPost_thumbnail() method i thought it was perfect for me, but then i saw that it requires a MediaItem object, and i dont know how to generate it correctly.

any other idea?

Thanks!!

I just stumbled upon your library and ran into the same problem thus i can confirm that there really seems to be a bug in the setPost_thumbnail method (tested on Wordpress 4.7.3). After setting a MediaItem with a valid attachment id the file wp-includes/class-wp-xmlrpc-server.php reports an invalid attachment id because it is expecting an integer instead of an array in line 1396.

elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) )
    return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );

I just edited line 238 in XmlRpcMapped.java into this quick and dirty fix and it worked:

if (field.getName().equals("post_thumbnail"))
  result.put(field.getName(), ((MediaItem)o).getAttachment_id());
else
  result.put(field.getName(), o);

I did not check this for older Wordpress versions, but you could easily workaround this with some version checks, weather there is a need to support older ones.

Thanks, do you have a pull request for this?

request is out - if you like, you can merge it now.

I solved it a bit more cleanly inside the post object itself. Thank you for finding it out.