*****编辑***如果我能想出如何在父帖子更新时自动更新子帖子,下面的第一个功能将起作用**
我希望有人能帮助我。我在Google上搜索了这么多,以至于Google把我定位为一个机器人,我不得不填写大约10个验证码来验证我是人类。
到目前为止,我有以下内容,仅在更新子帖子时更新子帖子类别以匹配父帖子类别,但我需要在更新父帖子时进行更新,否则每当我更改父帖子时,我必须手动更新每个子帖子。
/** Set Child Terms to Parent Terms on Publish **/
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 );
我想这样的东西可能会有用,但我真的不知道该怎么做才能让它发挥作用。我肯定我在这里做错了很多事情。
//Hopefully update the child cats when parent post is updated
function update_child_cats( $post_id, $post ){
if ( \'publish\' === $post->post_status && $post->post_parent = 0 ){
function wp_get_post_categories( $post_id = 0, $args = array() ) {
$post_id = (int) $post_id;
$defaults = array(\'fields\' => \'ids\');
$args = wp_parse_args( $args, $defaults );
$cats = wp_get_object_terms($post_id, \'category\', $args);
return $cats;
}
$args = array(
\'post_parent\' => 0,
\'fields\' => \'ids\',
);
$ChildIDs = get_children( $args );
$ParentCategoryIDs = $cats;
// Update post 37
$my_post = array(
\'ID\' => array($ChildIDs), //$ChildIDs returned from children above
\'cat\' => array($ParentCategoryIDs), //returned from above.
);
// Update the post into the database
wp_update_post( $my_post );
}
}
add_action( \'post_updated\', \'update_child_cats\', 100, 2 );