我不明白为什么这段代码不能作为数组使用:
function remove2categories( $new_status, $old_status, $post ) {
if ( $old_status == \'private\' && $new_status = \'publish\' && !in_category(array(\'3152\'), $post ) ) {
$catsID = array(\'1186\',\'1208\');
wp_remove_object_terms( $post->ID, $catsID, \'category\' );
}
}
add_action( \'transition_post_status\', \'remove2categories\', 10, 3 );
然而,这段代码工作得非常好(没有数组)。。。仅删除2个类别ID中的1个
function remove1categories( $new_status, $old_status, $post ) {
if ( $old_status == \'private\' && $new_status = \'publish\' && !in_category(array(\'3152\'), $post ) ) {
wp_remove_object_terms( $post->ID, 1186, \'category\' );
}
}
add_action( \'transition_post_status\', \'remove1categories\', 10, 3 );
这里我做错了什么,当帖子状态从私有更改为发布时,如何让代码删除这两个类别ID?
最合适的回答,由SO网友:Shibi 整理而成
这是因为您试图传递字符串数组而不是int。
因此,它搜索具有此值的术语slug,而不是在术语id中。
function remove2categories( $new_status, $old_status, $post ) {
if ( $old_status == \'private\' && $new_status == \'publish\' && !in_category(array(3152), $post ) ) {
$catsID = array(1186, 1208);
wp_remove_object_terms( $post->ID, $catsID, \'category\' );
}
}
add_action( \'transition_post_status\', \'remove2categories\', 10, 3 );