简体   繁体   English

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

[英]Python - python-wordpress-xmlrpc add tags to a post with known id

I have a python list with tags and the id of the post I want to add the tags to. 我有一个带有标签的python列表和要将标签添加到的帖子的ID。 I do not retrieve the post id with wordpress-xmlrpc. 我没有使用wordpress-xmlrpc检索帖子ID。

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

According to documentation here I should use something like: 根据这里的文档我应该使用类似的东西:

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

But I have already the post id, I just want to update it with my tags_list (which I don't know If they are already created in wp database or not) 但是我已经有了帖子ID,我只想用我的tags_list更新它(我不知道它们是否已经在wp数据库中创建)

Try this 尝试这个

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

Another way ,mysql: the relationship is very simple , I fix my problems via sql . 另一种方式,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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM