简体   繁体   中英

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. I do not retrieve the post id with wordpress-xmlrpc.

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)

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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