自定义帖子类型和作者不相关,用户帖子数为0,api不会在帖子对象中返回作者
问题描述:
当用户创建自定义帖子类型时,作者字段似乎不会被传递回WP帖子功能。自定义帖子类型和作者不相关,用户帖子数为0,api不会在帖子对象中返回作者
正如标题所说,我以编程方式创建用户帐户,并让该用户登录并写入自定义post_type = 'job'
的帖子。
这一切都看好数据库 - 用户和帖子都被插入。 post_author
确实与创建它的用户的ID
相同。
但是,在用户帐户面板中,该用户发布计数为0,并且API数据对象省略了作者。我已经为其他自定义帖子类型配置了API,并且端点的作用是返回作者以外的所有帖子数据。
我试着从这些用户创建一个CMS的帖子,同样,即使我给他们管理员权限,他们有一个0职位数 - 帖子归因于他们的作者ID。我也试图强迫和更新使用wp_update_post();
下面是创建用户的代码,然后邮寄:
// Generate the password and create the user
$password = wp_generate_password(12, false);
$user_id = wp_create_user($email_address, $password, $email_address);
//login
wp_clear_auth_cookie();
wp_set_current_user ($user_id);
wp_set_auth_cookie ($user_id);
// Set the role
$user = new WP_User($user_id);
$user->set_role('subscriber');
//Ready data for linked post type creation
$new_post = array(
'post_content' => $email_address,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_id,
'post_title' => $post_name,
'post_name' => $post_name,
'post_type' => $user_type
);
//Create the post
$account_link_post_id = wp_insert_post($new_post);
答
有一个相关的帖子这一项,在这个答案线索。在注册帖子类型时,我没有在register_post_type
的'supports'数组中设置足够的字段。添加author
,whatdayaknow,给我我正在寻找的数据点。
register_post_type(self::POST_TYPE,
array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'description' => __(""),
'supports' => array(
'title', 'author'
),
)
)
注:职位数量的用户帐户页面仍然是0 - 但是,该API将返回我想要的数据/需要。