Wordpress 有没有办法让子帖子继承父帖子条款?

Wordpress 有没有办法让子帖子继承父帖子条款?,wordpress,Wordpress,我有一些从父母到大孙子的自定义帖子。我为父帖子指定了一个自定义分类法中的术语,我希望所有子帖子都继承该术语。有办法做到这一点吗?否则,我将不得不将该术语应用于每篇文章,这将是一项非常艰巨的任务。如果您从wordpress ui创建文章,只需将其添加到functions.php中,它就可以满足您的需要: function set_parent_terms( $post_id, $post ) { if ( 'publish' === $post->post_status &&

我有一些从父母到大孙子的自定义帖子。我为父帖子指定了一个自定义分类法中的术语,我希望所有子帖子都继承该术语。有办法做到这一点吗?否则,我将不得不将该术语应用于每篇文章,这将是一项非常艰巨的任务。

如果您从wordpress ui创建文章,只需将其添加到functions.php中,它就可以满足您的需要:

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 );

如果您使用
wp\u insert\u post
创建帖子,那么我真的不知道如何使其工作我发现上面的代码很有用,并对其进行了调整,以便将父对象保存到子对象,以防对任何人都有用(这似乎更符合OPs用例,因为每次他们添加一个父术语时,它都会在save上向下延伸)

函数集\u子\u术语($post\u id,$post){
$args=数组(
“post\u parent”=>$post\u id,
“post_类型”=>“产品”,
“numberposts”=>-1,
“post_状态”=>“任何”
);
$children=get_children($args);
如果('publish'===$post->post_状态&&$children>0){
foreach((数组)$children作为$child){
如果(!空($child)){
$taxonomies=get\u object\u分类($post->post\u type);
foreach((数组)$taxonomy作为$taxonomy){
$terms=wp\u get\u post\u terms($post->ID,$taxonomy);
如果(!空($terms)){
$termArr=array_map(创建_函数('$obj','return$obj->term_id;'),$terms);
$tmp=wp\u set\u object\u terms($child->ID,$termArr,$taxonomy,false);//如果不想覆盖/重置现有术语,请设置为true
}
}
} }
}
}
添加操作('save_post','set_child_terms',100,2);

?>
Research@brasofilo谢谢你的建议。但是,我在发布之前浏览了网页,没有找到解决方案。好的。如果你将此类信息添加到你的问题中会更好,这样你就不会收到你已经尝试过的建议。