admin管理员组

文章数量:1402964

When Users create custom post types, the author field does not seem to be passed back to WP posts functions.

As the title says, I'm creating a user account programatically, and having that user log-in and write a post of custom post_type = 'job'.

This all LOOKS good in the database - the user, and post are inserted. The post_author is indeed the same as the ID of the user who created it.

But, in the User Accounts panel, that user post count is 0, and the API data object omits the author. I've configured the API for the additional custom post type and the endpoint works to return all post data EXCEPT author.

I've tried creating a post from the CMS with these users as well, and likewise, even if I give them Administrator permissions, they have a 0 post count - the posts are attributed to their author ID. I've also tried to force and update using wp_update_post();

Here's the code to create the user, and then the 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);

ANY suggestions here would be great. Thank you in advance.

When Users create custom post types, the author field does not seem to be passed back to WP posts functions.

As the title says, I'm creating a user account programatically, and having that user log-in and write a post of custom post_type = 'job'.

This all LOOKS good in the database - the user, and post are inserted. The post_author is indeed the same as the ID of the user who created it.

But, in the User Accounts panel, that user post count is 0, and the API data object omits the author. I've configured the API for the additional custom post type and the endpoint works to return all post data EXCEPT author.

I've tried creating a post from the CMS with these users as well, and likewise, even if I give them Administrator permissions, they have a 0 post count - the posts are attributed to their author ID. I've also tried to force and update using wp_update_post();

Here's the code to create the user, and then the 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);

ANY suggestions here would be great. Thank you in advance.

Share Improve this question edited Aug 30, 2017 at 14:05 lakewood asked Aug 30, 2017 at 2:44 lakewoodlakewood 1416 bronze badges 4
  • How are you calling this code, in an action? – Jim Maguire Commented Aug 30, 2017 at 3:09
  • This code is in a function that is called with AJAX on a form submit. add_action( 'wp_ajax_createAccount', 'var_fn_createAccount' ); add_action( 'wp_ajax_nopriv_createAccount', 'var_fn_createAccount' ); function var_fn_createAccount() { ... } – lakewood Commented Aug 30, 2017 at 11:52
  • The count shown on the the user's table is only the post post type. The API issue sounds like a capabilities problem. – Milo Commented Aug 30, 2017 at 20:35
  • @milo You're right! I found through other posts that I was not setting 'author' as an available field in the Post Type registration. – lakewood Commented Aug 30, 2017 at 22:34
Add a comment  | 

2 Answers 2

Reset to default 0

There was an associated post to this one that shed light on this answer. When registering the post type, I was not setting enough fields in the 'supports' array of the register_post_type. Adding author, whatdayaknow, gives me the data I'm looking for.

Thanks to @milo for being on the right path!

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

NOTE: Post count in the User Accounts page is still 0 - but, the API is returning the data I want/need.

Get Count of Custom post in your author page try this code.

This goes in your functions.php

function rt_count_user_posts( $userid, $post_type ) {
if( empty( $userid ) )
   return false;

$args = array(
    'post_type' => $post_type,
    'author'    => $userid
);

$the_query = new WP_Query( $args );
$user_post_count = $the_query->found_posts;

return $user_post_count;
}

and in your author.php use this function to get author's count of the custom post.

$count = rt_count_user_posts($authorId, 'your-custom-post');

本文标签: