Setting author when creating a new node via Cron I'm creating a new node using hook_cron(). It all works except the node author is always set as anonymous (uid = 0). How can I assign a user to the new node, with say uid 21 for example?

Below is the node creation code that I have in a function that gets called within hook_cron().

$node = new stdClass();
$node-&gt;type = $type;
$node-&gt;title = "Some node title";
$node-&gt;language = LANGUAGE_NONE;
$node-&gt;uid = 21;
node_object_prepare($node);

$node-&gt;body[$node-&gt;language][0]['value'] = '';
$node-&gt;body[$node-&gt;language][0]['summary'] = '';
$node-&gt;body[$node-&gt;language][0]['format'] = 'html_text';

$node-&gt;field_quote[$node-&gt;language][0]['value'] = $some_value;
$node-&gt;field_quote[$node-&gt;language][0]['format'] = 'plain_text';

$node = node_submit($node);
node_save($node);


Even though I state $node-&gt;uid = 21;, it always results in an anonymous author.
You have two options.

Either set the uid after the node_object_prepare call and before node_save, or you can set $node-&gt;name to the username of the user that should be the author.

The reason is that node_submit will set the uid of the node based on the value of $node-&gt;name if set and node_object_prepare will always set author to the current user.