title: Drupal 7 programmatic field definition
body: <p>I need help programmatically setting up a <strong>node reference</strong> field.</p>
  
  <p>My module successfully creates a and associates a pair of 'CCK' fields to my nodes. One of these fields is a <strong>node_reference</strong> field. My code is a follows:</p>
  
  <pre><code>$field_ref_name = 'field_custom_reference';
  $field = field_info_field($field_ref_name);
  
  if (empty($field)) {
      $field = array(
          "field_name"=&gt;$field_ref_name,
          "label"=&gt;"Custom Reference",
          "type"=&gt;"node_reference",
          "cardinality"=&gt;"1",
          'locked' =&gt; TRUE,
      );
      field_create_field($field);
  }
  
  $instance = array(
      "field_name"=&gt;$field_ref_name,
      "label"=&gt;"Sequence Reference",
      "type"=&gt;"node_reference",
      "widget"=&gt;array(
          "type"=&gt;"node_reference_autocomplete"
      ),
      "description" =&gt; "text describing purpose of this field",
  );
  
  $instance["entity_type"] = "node";
  $instance["bundle"] = $type;
  
  if( !in_array($type, $field['bundles']['node']) )
      field_create_instance($instance);
  </code></pre>
  
  <p>Now, the code works but when I edit a node inputting a valid value into the node reference field and attempt to save, I get the following error:</p>
  
  <blockquote>
    <p>...: this post can't be referenced.</p>
  </blockquote>
  
  <p>I realized the reason for the error is because the node reference field settings does not have any selected nodes as "<strong>Content types that can be referenced</strong>".</p>
  
  <p>Does anyone know how I can adjust my code to set referenceable content types?</p>