在注册特定用户角色时创建自定义帖子类型

时间:2020-05-27 作者:user3626802

有谁能帮我在注册新用户时创建自定义帖子类型的内容?但是用户应该只具有特定的角色。

我一直在搜索,但找不到任何解决方案。

1 个回复
SO网友:Yash Tiwari

Try below code:

add_action( \'user_register\', \'create_new_post_onuser_registration\', 10, 1 );
function create_new_post_onuser_registration( $user_id ){
    // Get user info
    $user_meta = get_userdata( $user_id );

    $user_roles = $user_meta->roles;

    // Update the role here
    if ( in_array( \'subscriber\', $user_roles ) ) {
        // Do something.

        // Create a new post
        $subscriber_post = array(
            \'post_title\'   => $user_meta->nickname;
            \'post_content\' => $user_meta->description,
            \'post_type\'    => \'custom_post_type\', // Update to your custom post type
        );

        $post_id = wp_insert_post( $subscriber_post );

        // Add custom data
        add_post_meta( $post_id, \'user_email\', $user_meta->user_email );
    }    
}

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在