簡體   English   中英

Python-python-wordpress-xmlrpc將標簽添加到具有已知ID的帖子中

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

我有一個帶有標簽的python列表和要將標簽添加到的帖子的ID。 我沒有使用wordpress-xmlrpc檢索帖子ID。

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

根據這里的文檔我應該使用類似的東西:

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數據庫中創建)

嘗試這個

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

另一種方式,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