【问题标题】:Custom Post Type & Author not associating, user post count is 0, api doesn't return author in post objects自定义帖子类型和作者未关联,用户帖子计数为 0,api 不返回帖子对象中的作者
【发布时间】:2018-02-07 14:50:08
【问题描述】:

当用户创建自定义帖子类型时,作者字段似乎没有传递回 WP 帖子功能。

正如标题所说,我正在以编程方式创建一个用户帐户,并让该用户登录并撰写自定义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);

【问题讨论】:

    标签: wordpress wordpress-theming posts wordpress-rest-api author


    【解决方案1】:

    有一篇与此相关的帖子阐明了这个答案。注册帖子类型时,我没有在register_post_type 的“支持”数组中设置足够的字段。添加author,whatdayaknow,为我提供了我正在寻找的数据点。

    register_post_type(self::POST_TYPE,
      array(
       'labels' => $labels,
       'public' => true,
       'has_archive' => true,
        'description' => __(""),
           'supports' => array(
              'title', 'author'
        ),
      )
    )
    

    注意:用户帐户页面中的帖子计数仍为 0 - 但是,API 正在返回我想要/需要的数据。

    【讨论】:

    • 在用户帐户页面中显示正确帖子计数的任何解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2017-02-13
    相关资源
    最近更新 更多