Programmatically creating a node I am trying to create content from a cron job with a custom node type that contains just a title and body; the content type is mycontent (for argument sake).

I am trying to use the Entity API module because apparently it is supposed to be simpler.

Working from examples, I wrote the following code.

$values = array(
    'type' =&gt; 'mycontent',
    'uid' =&gt; $user-&gt;uid
  );
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper-&gt;title-&gt;set('My Title');


Clearly I am missing something here because I get an exception (EntityMetadataWrapperException: Unknown data property title).

So I changed it to the following.

$values = array(
    'type' =&gt; 'mycontent',
    'uid' =&gt; $user-&gt;uid
  );
$entity = entity_create('mycontent', $values);
$ewrapper = entity_metadata_wrapper('mycontent', $entity);
$ewrapper-&gt;title-&gt;set('My Title');


Now I get a different error, an obtuse Invalid data value given.

Why cannot I get the Entity API module to work as advertised?

The current user in this case is user 1, not the anonymous user. If I add the content using Drupal standard functions, it works. So I know I am not having a permission issue.

I looked at this example, to write my code, and then proceeded to google a lot.
This will work for you:

$node = new stdClass();
$node-&gt;title = "My title";
$node-&gt;type = "mycontent";
node_object_prepare($node); 
$node-&gt;uid = $user-&gt;uid; 
$node-&gt;status = 1;
$node = node_submit($node);
node_save($node);


More details you can see at
https://www.drupal.org/docs/7/howtos/create-a-node-in-code