Create content of different entity by user programatically I am using an OpenSocial site, and writing a custom code that will create a post content by specific user. The post was created but assigning user is not working. How to properly assign userID to a post entity type?

use Drupal\social_post\Entity\Post;
...
    $node = Post::create([
      // The node entity bundle.
      'type' =&gt; 'post',
      'field_post' =&gt; $post,
      'field_visibility' =&gt; 1,
      'uid' =&gt; 2, // user = John
    ]);
    $node-&gt;save();


Actual result:

POST ID  POST   AUTHOR  CREATED                   OPERATIONS
1        Test   admin   Wed, 04/18/2018 - 10:19   Edit


Expected result:

POST ID  POST   AUTHOR  CREATED                   OPERATIONS
1        Test   john    Wed, 04/18/2018 - 10:19   Edit
The key is user_id, so presumably the correct code would be:

$post = Post::create([
  'type' =&gt; 'post',
  'field_post' =&gt; $post,
  'field_visibility' =&gt; 1,
  'user_id' =&gt; 2,
]);

$post-&gt;save();


I changed the var name to $post as it's not a node and you don't want to confuse those two things. I also removed the type as the social post entity type doesn't have such a field.