How to create or update node via custom code in drupal

    To create custom node in drupal via code follow the steps:
    <?php
        $node
    = new stdClass();
       
    $node->type = 'job'; //Content Type of node
       
    $node->status = 1;
       
    $node->created = time();
       
    $node->changed = time();
           
    $node->title = "Custom Node";
           
    $node->body = "this is test job description";
          
    //In case of cck fields $node->job_company[0]['value'] = "Test"; like this
       
    node_save($node);
    ?>

    To update custom node in drupal via code follow the steps:
    <?php
           
    //In case of update we have nid so that we can load earlier node change values and then save
       
    $node = node_load($earlier_nid);
           
    $node->title = "Custom Node";
           
    $node->body = "this is test job description";
           
    //In case of cck fields $node->job_company[0]['value'] = "Test"; like this
       
    node_save($node);
    ?>
    5
    Average: 5 (1 vote)