I had made a custom form and want a field in it that would work as a node reference field. Currently iam not on the node page but i want the nid of nodes of a content type to be the options of the select field which is my node reference field. here is the code for the select field that should work as node reference field.

function formModule_form($form, &$form_state) {

    $form['selectbox'] = array(
    '#type' => 'select', 
    '#title' => t('What is your Qualification?'), 
    '#required' => TRUE,
    '#options' => array(
      '1' => 'UnderGraduate',
      '2' => 'Graduate',
      '3' => 'Post Graduate',
    '#description' => t('Choose an option.'),

return $form

the #option in the above code should have the nid of nodes.

  • if you aren't node viewing, then where are you and how you want determine parent node nid?
    – Nikit
    Feb 1, 2013 at 6:27

3 Answers 3


You'll want to Query the database for the nodes you need and then use the results of that query to populate the #options property. Something like this:

    $result = db_select('node', 'n')
    ->condition('type', 'node_type_name','=')
    ->condition('status', 0,'>')

You can then use the results of that query easily since it comes back as an associative array. You'll want to specify exactly which fields you want (in your case, probably just title and node. See http://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7 for how to do a proper select.


You could call a simple query and create the option list something like this

    $result = db_query("SELECT n.nid, n.title FROM {node} n 
                         WHERE n.type = 'content type name'");

    $options = array();
   foreach($result as $row) {
      $options[$row->nid] = $row->title;

Now you can assign '#options' => $options

  • this code is giving an error:Fatal error: Call to undefined function db_fetch_object() in D:\xampp\htdocs\olinestore\store\sites\all\modules\formModule\formModule.module on line 56
    – aashi
    Feb 1, 2013 at 6:33
  • Sorry that was for drupal 6. for 7 you can use the for loop. have edited the code. Feb 1, 2013 at 6:35
  • by the way you dont need the node_load function just directly assign it to $row->title as we are getting it from query. have updated the answer Feb 1, 2013 at 6:47
  • you can use the forloop since the result implements the Iterator interface php.net/manual/en/class.iterator.php Feb 1, 2013 at 6:50

If you're in Drupal 7, I'm a big fan of Entity Field Queries. To get a list NIDs of all nodes of type article, that are published, and have a value in field_foo of "bar":

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'article')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_foo', 'value', 'bar', '=')
$result = $query->execute;

EFQs enable you to write queries without knowing the details of the table and column names, and you can also run them with the permissions of a specific user. They return an array of entity IDs so they're ideal for just finding the NID of a node.

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.