【问题标题】:Wordpress xmlrpc python check if post title existsWordpress xmlrpc python检查帖子标题是否存在
【发布时间】:2018-09-11 03:25:06
【问题描述】:

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

我有这个 sn-p 的代码,但它已经很老了,而且 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 中。

【问题讨论】:

    标签: python wordpress python-3.x xml-rpc


    【解决方案1】:
    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
    

    希望这对将来的某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多