set featured image for WordPress post via XML-RPC

血红的双手。 提交于 2019-12-20 01:40:56

问题


About 6 months ago there was an update to the WordPress API which allows setting a post's thumbnail (or featured) image.

http://www.maxcutler.com/2012/04/04/xml-rpc-in-wordpress-3-4/

I'm trying to use it but it's not working for me. I'd like to know what I may be doing wrong. I'm calling the XML-RPC newPost method to create a post and passing the Media ID of an existing asset in the media library (known as the attachment_id in the media library) The new post is being created and all the other properties are being set, except for the featured image.

I verified my version of the wordpress api, and sure enough in class-wp-xmlrpc-server.php I see the comment in the new post function section: "* post_thumbnail - ID of a media item to use as the post thumbnail/featured image"

All the other properties are working. I can add new images to the media library via XML-RPC. I can create and update posts and set their tags, titles, descriptions, custom field values, and categories. I don't get any errors when I try to set the post_thumbnail value. Even if I pass in a non-existent media id, which seems odd.


回答1:


arrrg! this WP version 3.4 ticket is misleading! http://core.trac.wordpress.org/ticket/20396

it's "wp_post_thumbnail" not "post_thumbnail"




回答2:


I was trying to do the same with my Ruby script and using XML RPC API.

  • First Initialize and get connect to your wordpress site:

    wp = Rubypress::Client.new( :host => "your host",
        :username => "test",
        :password => "test",
        :path => "yourhost/xmlrpc.php"
      )
    
  • Upload an image which you want as featured image.

    wp.uploadFile( :data => { :name => File.basename(FILENAME),
                  :type => "image/png",
                  :bits => XMLRPC::Base64.new(File.open(FILENAME).read)
                }
              )
    
  • get attachment id using getMediaItem method.

    attach = wp.getMediaItem(:blog_id => 0, :attachment_id => img_id.to_i)
    
    • Now create a post using newPost method

      wp.newPost( :blog_id => 0, # 0 unless using WP Multi-Site, then use the blog id
      :content => {
                   :post_status  => "draft",
                   :post_date    => Time.now,
                   :post_content => "This is the body",
                   :post_title   => "test title best!",
                   :post_name    => "test best",
                   :post_author  => 1,
                   :post_type=>'post',
                   :post_thumbnail => attach['attachment_id']
                   :terms_names  => {
                      :category   => ['Category One','Category'],
                      :post_tag => ['Tag One','Tag Two', 'Tag Three']
                                    },
      
                   }
      
      
      )
      
  • Check the result by getPost method which will return you the post

     get_data = wp.getPost(:post_id => new_post_resp.to_i, :blog_id => 0)
    

You should Refer the following links. These all are my findings when I was facing the same issue:

  • Reference-1
  • Reference-2
  • Reference-3
  • Reference-4
  • Reference-5


来源:https://stackoverflow.com/questions/12355922/set-featured-image-for-wordpress-post-via-xml-rpc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!