WordPress设置发布条款和 - 添加操作 - 保存发布问题

时间:2015-01-12 05:45:45

标签: wordpress function variables

尝试通过添加操作(保存帖子)挂钩

将自定义分类术语设置为帖子

这是我的代码

function dosomething(){

global $post, $wpdb;
$id = $post->ID;
global $id; // using id somewhere else 
$cat_ids = array( 45,35 ); // will be vars at somepoint in the future 

wp_set_post_terms( $id , $cat_ids, 'OriginalTag'); 
}

add_action('save_post', 'dosomething',10); // this will run the function on page load 

这对我不起作用?但通过反复试验我发现,如果我将set_post_terms函数中的$ id替换为帖子的实际值,如下所示:

wp_set_post_terms( 2154 , $cat_ids, 'OriginalTag'); //2154 being the post ID

一切正常......:/无法弄清楚我在这里做错了什么

P.S。我已经回显了$ id,它确实返回了正确的值

1 个答案:

答案 0 :(得分:0)

function dosomething($id){
    $cat_ids = array( 290, 44 ); // will be vars at somepoint in the future
    wp_set_post_terms( $id , $cat_ids, 'post_tag');
}

add_action('save_post', 'dosomething', 10, 1); // this will run the function on page load

这对我有用,显然我已经替换了我自己的标签ID并使用了默认的分类标准,所以请自己重新插入。我通过新帖子的$id通过动作调用,而不是对我来说也不起作用的全局变量。