我还没有对此进行过广泛的测试,但它在发布和更新方面都起到了作用。发布或更新帖子时,它会检查是否设置了父项,获取父项以获取所有父项分类法,然后循环并按分类法将子项分类法设置为父项分类法。
/** Set Child Terms to Parent Terms **/
function set_parent_terms( $post_id, $post ) {
if ( \'publish\' === $post->post_status && $post->post_parent > 0 ) {
$parent = get_post($post->post_parent);
if(!empty($parent)){
$taxonomies = get_object_taxonomies( $parent->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $parent->ID, $taxonomy );
if ( !empty( $terms ) ) {
$termArr = array_map(create_function(\'$obj\', \'return $obj->term_id;\'), $terms);
$tmp = wp_set_object_terms( $post_id, $termArr, $taxonomy, true );
}
}
}
}
}
add_action( \'save_post\', \'set_parent_terms\', 100, 2 );