python-wordpress-xmlrpc icon indicating copy to clipboard operation
python-wordpress-xmlrpc copied to clipboard

EditPost clears thumbnail

Open grinsted opened this issue 9 years ago • 7 comments

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))

grinsted avatar Mar 14 '16 08:03 grinsted

I have the same problem :(

Aref-Riant avatar Apr 08 '18 09:04 Aref-Riant

Save attachment id while iterating posts and reassign it.

n3storm avatar Apr 11 '18 16:04 n3storm

didnt worked @n3storm

Aref-Riant avatar Apr 14 '18 13:04 Aref-Riant

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.

n3storm avatar Apr 17 '18 16:04 n3storm

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))

Aref-Riant avatar Apr 18 '18 07:04 Aref-Riant

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.

maxcutler avatar Apr 18 '18 16:04 maxcutler

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

Aref-Riant avatar Apr 22 '18 10:04 Aref-Riant