WordPress设置发布条件并-添加操作-保存发布问题
试图通过addactionsavepost钩子为帖子设置自定义分类术语 这是我的密码WordPress设置发布条件并-添加操作-保存发布问题,wordpress,function,variables,Wordpress,Function,Variables,试图通过addactionsavepost钩子为帖子设置自定义分类术语 这是我的密码 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
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替换为post的实际值,如下所示:
wp_set_post_terms( 2154 , $cat_ids, 'OriginalTag'); //2154 being the post ID
一切正常…:/我想不出我做错了什么
p、 我回显了$id,它确实返回了正确的值
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放回去。我通过行动电话传递新帖子的$id,而不是globals,这对我也不起作用