一些wordpress主题使用标签(例如,“特色”)在首页显示有特色的内容。
我有一个python脚本,如果post中的某些custom_field值高于定义的值,它会标记所有带有“Feature”标记的帖子。该脚本每次运行时都会减少这些值。现在,如果这个custom_field低于定义的值,我想从帖子中删除‘defined’标签。
要为现有的post添加标记,我使用:
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替换或删除现有中的标记。
类似于: remove_tag(postID,'tag')
谢谢
编辑:
似乎不可能使用从post id ID定义的post中删除标记(或标记)。应该可以使用数据库查询(Wordpress -通过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/
发布于 2018-07-02 19:00:58
如果您能够向wordpress实例中添加代码/插件,那么您可以使用自定义XML-RPC方法来处理这个问题。
在Wordpress中,您需要添加一个函数来处理服务器端的魔术。(我已经将此添加到functions.php中,但更好的WP开发人员可能知道更好的位置。(也许是插件。)
我相信您需要wp_remove_object_terms来回答您的问题,但是当然您的自定义xml函数可以做任何您喜欢的事情。我使用这种方法删除自定义字段,并与我安装的另一个插件进行交互。
function xmlrpc_add_method_delete_tags( $methods ) {
$methods['my_namespace.delete_tags'] = 'xmlrpc_delete_tags_handler';
return $methods;
}
add_filter('xmlrpc_methods', 'xmlrpc_add_method_delete_tags');
function xmlrpc_delete_tags_handler( $args ) {
$result = array("ok" => false);
try {
// inspect $args for your parameters. I like to validate with regex,
// but they should just be at the end of $args
$n = count($args);
$postId = $args[$n-3];
$term = $args[$n-2];
$taxonomy = $args[$n-1];
$del = wp_remove_object_terms( $postId, $term, $taxonomy );
$result["ok"] = ($del === true);
if(is_wp_error($del)) {
$result['wp_errors'] = $del->get_error_messages();
}
}
catch(Exception $ex) {
$result["msg"] = $ex->getMessage();
}
return json_encode($result); // send a json response
}在Python中,定义一个wordpress_xmlrpc.AuthenticatedMethod (如果不需要身份验证,则定义AnonymousMethod )。
import json
from wordpress_xmlrpc import Client, AuthenticatedMethod
client = Client(WP_XMLRPC_URL, WP_USER, WP_PASSWORD)
class WordpressRemoveTag(AuthenticatedMethod):
# method_name must match the key string you added to $methods in WP
method_name = 'my_namespace.delete_tags'
method_args = ('postId', 'term', 'taxonomy')
def remove_tag_from_obj(obj_id, term, tax='post_tag'):
# return parsed json server response
res = client.call(WordpressRemoveTag(obj_id, term, tax))
return json.loads(res)然后在python中调用remove_tag_from_obj(...),您应该从服务器获得一个json响应:
print remove_tag_from_obj(post.id, 'my_tag')
>> {"ok":true}https://stackoverflow.com/questions/21047719
复制相似问题