Python - python-wordpress-xmlrpc将标签添加到已知id的帖子中

时间:2017-02-24 11:23:38

标签: python wordpress

我有一个包含标签的python列表以及我要添加标签的帖子的ID。我没有使用wordpress-xmlrpc检索帖子ID。

tags_list = ['tag1, 'tag2']
post_id = 107

根据文件here我应该使用类似的东西:

post = WordPressPost()
post.title = 'Post with new tags'
post.content = '...'
post.terms_names = {
        'post_tag': ['tagA', 'another tag'],
        'category': ['My Child Category'],
}
post.id = client.call(posts.NewPost(post))

但我已经发布了帖子ID,我只想用我的tags_list更新它(我不知道它们是否已经在wp数据库中创建了)

2 个答案:

答案 0 :(得分:0)

试试这个

currentPost=client.call(wordpress_xmlrpc.methods.posts.GetPost(1284))
cats=client.call(taxonomies.GetTerms('category',1024))//really do not know the second argument means ,so I pass 1024
catGif=None
catJoke=None
for cat in cats:
    if cat.name=='动态图':
        catGif=cat
    elif cat.name == '搞笑图片':
        catJoke=cat

tags=client.call(taxonomies.GetTerms('post_tag','1024'))
tagGif=None
tagJoke=None
for tag in tags:
    if tag.name=='动态图':
        tagGif=tag
    elif tag.name == '搞笑图片':
        tagJoke=tag

currentPost.terms.append(tagGif)
currentPost.terms.append(tagGif)
for term in currentPost.terms:
    print(term.name)
client.call(wordpress_xmlrpc.methods.posts.EditPost(currentPost.id,currentPost))

答案 1 :(得分:0)

另一种方式,mysql: 关系很简单,我通过sql解决了我的问题。

select * from wp_terms where name='动态图' or name='搞笑图片';

select * from wp_term_taxonomy where term_id in (select term_id from wp_terms where  name='搞笑图片')


select * from wp_term_relationships where term_taxonomy_id=332



SELECT * from wp_posts where ID>=827 and post_content like '%gif%'
and ID not in (select object_id from wp_term_relationships where term_taxonomy_id=208)

#动态图
insert into wp_term_relationships(object_id,term_taxonomy_id)
SELECT ID as object_id,208 as term_taxonomy_id from wp_posts where ID>=827 and post_content like '%gif%'
and ID not in (select object_id from wp_term_relationships where term_taxonomy_id=208)