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

Brilliant - работает как шарм! :) Спасибо. – richerimage

+0

Еще одна вещь - я хотел бы заполнить 'custom_field' идентификатором Authors - возможно ли это в этой функции? Благодарю. – richerimage

+0

@richerimage Обновлен ответ. –