does anyone know how to create node reference while creating node programmatically? I mean - using TITLE and TYPE, not nid.

I am creating nodes via python script and user is prompted for title of parent node. Nobody's gonna tell you NID in that case. But content type and part of title should be enough.

If there is no way of doing that in Drupal - I am gonna make my own function to fetch all nodes that fits criteria and go further if there's only one or prompt for choosing proper one if there's more than one..

I'll be happy if there's already something that handles my task.. :)

My current code with doing that by NID:

    $n = new stdClass();
    $n->title = "Kaboom [".date(YmdHis)."]";
    $n->language = LANGUAGE_NONE;
    $n->uid = 1;


    $path = 'remotely_added_at_'.date('YmdHis');
    $n->path = array('alias'=>$path);
    if($n=node_submit($n)) {
        echo "</br>Node with nid ".$n->nid." saved\n";


2 Answers 2


The only option you have is querying the node table to fetch the node id and re-ask if there are multiple matches yourself.

  • that's what I thought after checking google few times.. thanks.
    – wtk
    Oct 21, 2011 at 8:11

I don't think that you need to write your own function, just have a look at node_reference_autocomplete_value(), this functions does exactly what you need (match Node Title to nid)

  • thanks for a hint! any ideas how to get around the "form state" parameter? function looks like: function node_reference_autocomplete_value($element, $input = FALSE, $form_state) but i don't have form_state.. Some example of its usage would be highly appreciated ;) I don't like Drupal and its modules documentations..
    – wtk
    Oct 20, 2011 at 14:09
  • @wtk, which documentation are you looking at? The link in the answer shows function node_reference_autocomplete_value($element, $edit = FALSE), without a $form_state argument. Oct 20, 2011 at 14:52
  • maybe I have a newer version of Reference module? I go to mentioned file and this function looks as I pasted. What I see giving null as a third argument is fine, but I don't see any example of argument which we should pass to function. I managed to : $k = node_reference_autocomplete_value( array( '#default_value' => '85' ), FALSE, null ); print_r($k); which gives a title of node number 85, but I need a reverse of that ;) this is how the functions looks: wklej.org/id/611084 - which for me looks like searching for a NID, not a title?
    – wtk
    Oct 20, 2011 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.