Search code examples
pythonwordpresspython-3.xxml-rpc

Wordpress xmlrpc python check if post title exists


Making website for my town library using wordpress. The site will have several thousand posts, one for each book. I'm trying to get it so if that if a post with the same title already exists, it prints out something to let me know before I post it.

I have this snippet of code but it's pretty old and the documentation for xmlrpc wordpress especially with python is very lax.

post_id=find_id(post.title)
if post_id:
    print ("Sorry, we already have such a post" + post_id)
else:
    pass

This is the rest of my publishing code.

#client info#
wp = Client(wp_url, wp_username, wp_password)

post = WordPressPost()
post.title = 'Dracula'
post.post_status = 'draft'
post.terms_names = {
  'post_format': ['book'],
  'category': [tag],

}


post.custom_fields = []
post.custom_fields.append({'key':'dp_desc','value':desc})
post.custom_fields.append({'key':'fifu_image_url','value':thumb})

wp.call(NewPost(post))

Sorry if the answer exists already, all that I've seen have been in php.


Solution

  • from wordpress_xmlrpc import Client
    from wordpress_xmlrpc.methods import posts
    
    wp = Client(wp_url, wp_username, wp_password)
    posts = wp.call(posts.GetPosts())
    values = ','.join(str(v) for v in posts) # Changes list to a string
    

    Then you can just check the string for matches.

    if title in values:
        print('Post already exists!')
        continue
    else:
        pass
    

    Hope this helps someone in the future.