maxcutler / python-wordpress-xmlrpc

Python library for WordPress XML-RPC integration
http://python-wordpress-xmlrpc.rtfd.org
MIT License
381 stars 130 forks source link

EditPost clears thumbnail #96

Open grinsted opened 8 years ago

grinsted commented 8 years ago

This removed the featured images on all my posts:

allposts=client.call(posts.GetPosts({'number':1000}))

for post in allposts:
    post.content=re.sub(r"https?://debris.glaciology.net/","/",post.content);
    client.call(posts.EditPost(post.id,post))
Aref-Riant commented 6 years ago

I have the same problem :(

n3storm commented 6 years ago

Save attachment id while iterating posts and reassign it.

Aref-Riant commented 6 years ago

didnt worked @n3storm

n3storm commented 6 years ago

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.

Aref-Riant commented 6 years ago

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))
maxcutler commented 6 years ago

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.

Aref-Riant commented 6 years ago

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