0

少なくとも投稿者資格情報を持つユーザーごとに、カスタム投稿タイプ(jt_cpt_team)で投稿を自動的に作成することを検討しています。wp_insert_postを使用して各ユーザーの投稿を自動的に作成します

主に、管理者が作成した新しいユーザーごとにこれを行う必要があります。既存の著者(〜30)のそれぞれに対して投稿を生成する簡単な方法があれば、それは素晴らしいことですが、必要に応じて手作業で行うことは気にしません。

私はこれが多かれ少なかれ私が必要とするものだと推測しています—&hellipでも動作するように苦労しています。

class team_functions { 

    public function __construct() { 

    add_action('user_register', array($this, 'create_authors_page')); 

    } 

    public function create_authors_page($user_id) { 

    $the_user  = get_userdata($user_id); 
    $new_user_name = $the_user->user_login; 
    $my_post  = array(); 
    $my_post['post_title'] = $new_user_name; 
    $my_post['post_type'] = 'jt_cpt_team'; 
    $my_post['post_content'] = ''; 
    $my_post['post_status'] = 'publish'; 
    $my_post['post_theme'] = 'user-profile'; 

    wp_insert_post($my_post); 

    } 

} 

また、素晴らしいだろうcustom_fieldとして作者の電子メールを追加する方法があります場合。すべてのあなたの助けを事前に

感謝:)このような

答えて

1

何かが動作するはずです:ブリリアント

public function create_authors_page($user_id) { 

    $the_user  = get_userdata($user_id); 
    $new_user_name = $the_user->user_login; 
    $PostSlug  = $user_id; 
    $PostGuid  = home_url() . "/" . $PostSlug; 

    $my_post = array('post_title' => $new_user_name, 
         'post_type' => 'jt_cpt_team', 
         'post_content' => '', 
         'post_status' => 'publish', 
         'post_theme' => 'user-profile', 
         'guid'   => $PostGuid); 

    $NewPostID = wp_insert_post($my_post); // Second parameter defaults to FALSE to return 0 instead of wp_error. 

    // To answer your comment, adding these lines of code before the return should do it (Have not tested it though): 
    $Key = $new_user_name; // Name of the user is the custom field KEY 
    $Value = $user_id; // User ID is the custom field VALUE 
    update_post_meta($NewPostID, $Key, $Value); 

    return $NewPostID; 
    } 
+0

は - 魔法のように動作します! :) ありがとうございました。 – richerimage

+0

もう1つのこと - Authors IDで 'custom_field'を設定したいのですが、この関数内で可能ですか?ありがとう。 – richerimage

+0

@richerimage答えを更新しました。 –

関連する問題