Creating post using rest api and setting featured-image
-
Hi All,
I am using WordPress rest API to dynamically create my posts without going into the website.
I am able to OAuth2 and got the token too. I am able to create the post by sending the POST req on this link –
https://public-api.wordpress.com/rest/v1.2/sites/$siteID/posts/new
This is the req I am sending using request library in python-
`
my_image = (open(‘aa.jpg’, ‘rb’))
data = {‘slug’: ‘apitest’,
‘title’: title,
‘content’:”this is a new post created using rest api> It has tag, category, title”,
‘tags’: ‘restAPI’,
‘categories’: ‘Shayri’,
#’featured_image’: my_image,
}
files = {‘featured_image’: my_image,,
‘Content-Type’: ‘image/jpg’,
# ‘ID’:post_ID
}
response = requests.post(create_post_url, headers = {‘Authorization’: ‘Bearer ‘ + access_token},
data = data, files = files)I have gone through documents and saw that the featured image is set using a multipart-form post request This is what I am doing in the above code, but I am not able to set the featured image but I am able to set all other things.
Can anyone help with this? I am using python 3.9v and request library for HTTPS req.Thanks
The blog I need help with is: (visible only to logged in users)
-
Hello there,
Many thanks for reaching out.
I am using WordPress rest API to dynamically create my posts without going into the website.
Are you able to detail more about where you’re going to create posts please?
On first glance of the code, is the extra , in
files = {‘featured_image’: my_image,,There deliberately?
Many thanks in advance.
-
Hi,
I am creating posts on my website https://kavitalay.in. It is hosted on wordpress.com. I read that we can create new posts and manage posts all using wp-rest APIs.
This is the document for that- https://developer.wordpress.com/docs/api/1.2/post/sites/%24site/posts/%24post_ID/So far, I am able to create posts with the title, content, and few other things.
For creating the post, I am using python to send post request on wp-rest API.
This is my code-content = ‘my content test’
title = 'Test title' data = {'slug': 'apitest', 'title': title, 'content':content, 'tags': 'restAPI', 'categories': 'status', 'format' : 'Quote' } files = {'featured_image': my_image, 'Content-Type': 'image/jpg', 'Content-Length': 1, 'format' : 'image' } response = requests.post(create_post_url, headers = {'Authorization': 'Bearer ' + access_token}, data = data, files = files)So, from the documents, I have read that the featured image can be set when we do the multipart post request. And from the above code, I am doing sending the multipart data (files) in request to set my featured image.
But I am just able to create posts and the featured image I am not able to set.
My main concern is that I along with creating a new post, I want to set its featured image.Thanks,
akhi -
Hello Akhi,
Many thanks for getting back to me with that information.
I am creating posts on my website https://kavitalay.in. It is hosted on wordpress.com. I read that we can create new posts and manage posts all using wp-rest APIs.
Why are you looking to do this, rather than post on WordPress natively?
Many thanks in advance.
-
Hi,
Why are you looking to do this, rather than post on WordPress natively?
Because this is the requirement of us. I am automating my all posts so that later on anyone can post on this platform.
I don’t think you should worry about why rather than on the issue.Thanks
-
Hello there,
Many thanks for getting back to me.
Dealing with REST API is outside the scope of support can provide on WordPress.com, the writing these scripts falls under custom development.
A good place to look for a developer is UpWork: https://en.support.wordpress.com/upwork/
As you know, there’s also these docs here for reference: https://developer.wordpress.com/docs/api/1.2/
The reason why I ask about this requirement is because with a bit more context, we may be able to offer different insights.
I hope this is useful.
- The topic ‘Creating post using rest api and setting featured-image’ is closed to new replies.