Support

Account

Home Forums General Issues Custom field not populating on new post Reply To: Custom field not populating on new post

  • wp_current_user() returns a User object. To get the ID you would need to access it like:

    $current_user->ID

    So your code would be updated to:

    
    $current_user = wp_get_current_user();
    
    $new_post = array(
      'post_title' => 'Planner for '.$current_user->display_name,
      'post_type' => 'planner',
      'user_id' => $current_user->ID,
      'post_author' => $_SESSION['USER_ID'],
      'post_status' => 'publish'
    );
    
    $post_id = wp_insert_post( $new_post, $wp_error );
    
    if (is_wp_error($post_id)) {
      $errors = $post_id->get_error_messages();
      foreach ($errors as $error) {
        echo $error;
      }
    }