将已保存帖子的作者设置为其父帖子作者

时间:2017-06-28 17:01:11

标签: wordpress

我尝试用父母的作者改变保存的和声项目的作者但是那些功能似乎不起作用 - 空想我想。当我用恒定值设置作者id时,一切正常。

 $parent_post_id = wp_get_post_parent_id($post_id);

   $post_author_id = get_post_field( 'post_author', $post_id );

这是整个功能

    function func_auto_update_post_author( $post_id ) { 
    $post_type = get_post_type($post_id);

if ( "harmonogram" != $post_type ) return;

   //$parent_post_id = get_queried_object_id();
   $parent_post_id = wp_get_post_parent_id($post_id);

   $post_author_id = get_post_field( 'post_author',  $parent_post_id );
         $my_post = array(
            'ID'            => $post_id,
            'post_author'   => $post_author_id,  
        );

        remove_action('save_post', 'func_auto_update_post_author');
        wp_update_post($my_post);
        add_action( 'save_post', 'func_auto_update_post_author');
}
add_action( 'save_post', 'func_auto_update_post_author');

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

foreach($user in $users)
{
 Get-aduser -filter {DisplayName -like $user.Displayname} |Set-ADUser -Replace @{PhysicalDeliveryOfficeName="Van Nuys"}
}

您可以通过将以下代码放在上面的操作挂钩中来跟踪发布更新期间是否发生任何错误:

function func_auto_update_post_author( $post_id ) 
{ 
    $post_type = get_post_type($post_id);
    if ( "harmonogram" != $post_type ) return;
    $parent_post_id = wp_get_post_parent_id($post_id);
    if($parent_post_id)
    {
         $post_author_id = get_post_field( 'post_author', $parent_post_id );
         $my_post = array(
            'ID'            => $post_id,
            'post_author'   => $post_author_id
        );
        wp_update_post($my_post);
     }
}
add_action( 'save_post', 'func_auto_update_post_author');

希望这会有所帮助!

相关问题