Wordpress xmlrpc python 检查 post 标题是否存在

Wordpress xmlrpc python check if post title exists

使用 wordpress 为我的城镇图书馆制作网站。该站点将有数千个 post,每本书一个。我正在尝试获取它,如果已经存在具有相同标题的 post,它会在我 post 之前打印出一些内容让我知道。

我有这段代码,但它已经很旧了,而且 xmlrpc wordpress 的文档尤其是 python 非常松散。

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

这是我发布代码的其余部分。

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

抱歉,如果答案已经存在,我所看到的都在 php。

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

然后您可以只检查匹配的字符串。

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

希望这对以后的人有所帮助。