2017-06-28 6 views
0

親の作者で保存されたハーモノグラムの作者を変更しようとしていますが、機能に対するものは機能していないようです。私が定数値を持つ著者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'); 
+0

_ "空だと思う" _ - "考えない" - _verify_。 'var_dump'を使って変数に実際に何が含まれているのかを確認してください。 – CBroe

答えて

0

コードの下に試してみてください。

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

何らかのエラーが上記のアクションフックにコードの下に置くことで、ポスト更新時に発生した場合は、追跡することができます:

$post_id = wp_update_post($my_post, true);       
if (is_wp_error($post_id)) { 
    $errors = $post_id->get_error_messages(); 
    foreach ($errors as $error) { 
     echo $error; 
    } 
} 

希望はこのwi助けてください!

関連する問題