【问题标题】:How can I make the current user the post author? [closed]如何使当前用户成为帖子作者? [关闭]
【发布时间】:2020-11-24 12:45:44
【问题描述】:

我想在用户注册时自动创建帖子。我使用下面的代码,效果很好。唯一缺少的是帖子作者必须是创建注册的用户。

有人可以帮我添加一行以使帖子的作者成为当前用户吗?谢谢!

这是代码:

add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    if ($this_user_role == 'author') {

        // Create a new post
        $user_post = array(
            'post_title'   => $user_info->nickname,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'Team', // <- change to your cpt
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}

【问题讨论】:

    标签: php wordpress user-registration author


    【解决方案1】:

    我将'post_author' =&gt; '$user_id 添加到您的$user_postarray。

    // Create a new post
    $user_post = array(
       'post_title'   => $user_info->nickname,
       'post_status'  => 'publish', // <- here is to publish
       'post_type'    => 'Team', // <- change to your cpt
       'post_author'  => $user_id
       );
    

    【讨论】:

    • 乐于助人!请考虑接受这个或任何其他解决问题的答案
    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多