【发布时间】:2014-01-10 15:09:38
【问题描述】:
一些 wordpress 主题使用标签(例如“特色”)在首页上显示特色内容。
如果帖子中的某些 custom_field 值高于定义的值,我有一个 python 脚本,它用“特色”标签标记所有帖子。该脚本还会在每次运行时减少这些值。现在,如果此 custom_field 低于定义的值,我想从帖子中删除“精选”标签。
为我使用的现有帖子添加标签:
server = xmlrpclib.ServerProxy(wp_url)
client = Client(wp_url, wp_username, wp_password)
post = client.call(posts.GetPost(postID))
post.custom_fields = [{'id': featuredscoreID, 'key':'featuredscore','value': featscore}]
post.terms_names = {
'post_tag': ['new tag'],
}
post.call(posts.EditPost(postID, fpost))
但此代码只会附加标签。
print post.terms
会给我这样的东西:
[<WordPressTerm: Architecture>, <WordPressTerm: featured>, <WordPressTerm: Image>, <WordPressTerm: redevelopment>, <WordPressTerm: station>]
但我发现无法用 python 替换或删除现有帖子 wordpress 中的标签
类似:remove_tag(postID, 'tag')
谢谢
编辑:
似乎无法使用 python XML-RPC API 从帖子 ID ID 定义的帖子中删除标签(或标签)。应该可以使用数据库查询(Wordpress - Delete all tags by Post ID),但是这个特定的例子(对我来说)不起作用,如果需要从本地主机以外的其他位置运行这个(脚本),则需要放松 MySQL 安全性并打开一些端口。
因此,作为解决方案,从 wordpress 中删除标签,然后将其应用回应该在的帖子中。删除标签:
postTags = client.call(taxonomies.GetTerms('post_tag'))
for tag in postTags:
try:
if str(tag) == 'tag you would like to delete':
client.call(taxonomies.DeleteTerm('post_tag', tag.id))
except:
print "error"
自动“精选”帖子:http://imageoftheday.org/
【问题讨论】: