Create node programmatically with menu entries I created an action in Rules, which generates a duplicate of a node. If I am creating a node of the type "basic", it will duplicate the node for every taxonomy term. So when I have a node with three taxonomy terms, the action will generate three nodes.

The only difference are the taxonomy terms, because the created nodes should get the URL '/taxonomy-term1/node-title', '/taxonomy-term2/node-title', ...

My problem: If I have the taxonomy terms Taxonomy_Term1, Taxonomy_Term2 and Taxonomy_Term3, I get the following result:

/taxonomy-term1/node-title

/default/node-title

/default/node-title-0


I am thinking the problem lies in the array which was created from the latest nid. But when I am printing each term_name (to know what it looks like), the results are the Strings Taxonomy_Term1, Taxonomy_Term2 and so on.

function autocreatenode_rules_action() {
$nid = db_query_range("SELECT nid FROM {node} ORDER BY nid DESC", 0, 1)-&gt;   fetchField();
$string = token_replace('[node:field_tags]', array('node' =&gt; node_load($nid)));
$terms = explode(',', $string);
$i = 0;
foreach ($terms as $term_name) {
    $i++;
}
    if($i &gt;= 2) {
        foreach ($terms as $term_name) {
            $node = new stdClass(); 
            $node-&gt;type = 'basic';  
            $node-&gt;language = 'de';  
            node_object_prepare($node);  
            $node-&gt;title = 'Title';
            $node-&gt;field_body[LANGUAGE_NONE][0]['value'] = 'Something in the body';
            $menu = array();
            $menu['link_title'] = 'Menu Link title';
            $menu['description'] = '';
            $menu['menu_name'] = 'main-menu';

            switch($term_name) {
                case 'Taxonomy_Term1':
                    $menu['plid'] = 5;
                    break;
                case 'Taxonomy_Term2':
                    $menu['plid'] = 6;
                    break;
                case 'Taxonomy_Term3':
                    $menu['plid'] = 7;
                    break;
                default:
                    $menu['plid'] = 1;
            }

            $menu['enabled'] = 1;
            $node-&gt;menu = $menu;
            $node-&gt;status = 1;
            $node-&gt;promote = 0;
            $node-&gt;sticky = 0;
            $node-&gt;comment = 2;
            $node-&gt;uid = 1;
            $node-&gt;date = 'complaint_post_date';
            $node-&gt;created = strtotime('complaint_post_date');
            $node = node_submit($node);
            node_save($node);
        }
    }}
The code you posted seems a bit frail at places, but if it works for you, we can focus on your specific question.

The main problem is that you are not adding the $menu array to your node object before saving it. You would only need to add it to the object before you save it, similar to this:

$node-&gt;menu = $menu;
...
node_save($node);


Cheers and good luck!

P.S.: You can check out this thread for some code examples https://stackoverflow.com/q/2603289/5875098
P.S.2: To get all the terms directly from the $node object:

$terms = array();
$language = 'und'; // or will be provided by some Drupal hooks
foreach ($node-&gt;field_tags[$language] as $delta =&gt; $value) {
  $termObj = taxonomy_term_load($value['tid']);
  $terms[] = $termObj-&gt;name;
}


You can replace the $terms = explode... in your code with this snippet.