Open grinsted opened 8 years ago
I have the same problem :(
Save attachment id while iterating posts and reassign it.
didnt worked @n3storm
It's easy but not straight forward. Something like this:
For each post:
media_id = post.thumbnail
change your text
post.save()
post.thumbnail = media_id
post.save()
Or show the code not working for you.
Thanks for attention @n3storm Here is the code, not working...
def post_test1():
posts = client.call(GetPosts())
for p in posts:
if p.title.lower() == 'ballot':
thumb = p.thumbnail
p.title = 'Ballot'
result = client.call(EditPost(p.id, p))
p.thumbnail = thumb
result = client.call(EditPost(p.id, p))
return result
break
when there is a thumbnail on post, "thumb" variable is a dictionary like below:
{'attachment_id': '1234', 'date_created_gmt': <DateTime '20180324T15:28:01' at 0x14bfcc3c940>, 'parent': 1230, 'link':
,
but when it is removed, its type becomes as an empty array...
also i tried:
p.title = 'Ballot'
result = client.call(EditPost(p.id, p))
p.thumbnail = dict()
p.thumbnail['attachment_id'] = '1234'
result = client.call(EditPost(p.id, p))
This behavior is a quirk of how WordPress' XML-RPC API works.
For getPost
or getPosts
, the thumbnail field returns a struct/object: https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#Return_Values
But when using newPost
or editPost
, the thumbnail field should be the attachment ID: https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost. I tried to show this in the media example in the docs (last code block): http://python-wordpress-xmlrpc.readthedocs.io/en/latest/examples/media.html
So when calling EditPost
, the post object should have a thumbnail
field with the attachment's ID as the value.
Hey, thanks bro worked for me:
def post_test1():
posts = client.call(GetPosts())
for p in posts:
if p.title.lower() == 'ballot':
post = WordPressPost()
thumb = p.thumbnail['attachment_id']
print(thumb)
post.title = 'Ballot'
post.thumbnail = thumb
post.content = p.content
post.post_status = p.post_status
result = client.call(EditPost(p.id, post))
return result
break
This removed the featured images on all my posts: