[Drupal 6] How to fetch CCK fields value programmatically?

| | 1 min read

We might encounter a situation where we may need to fetch CCK fields values in a node, i.e, we may need to show CCK field values in custom search results. Drupal provides a easy way to fetch CCK fields programmatically. Read on to know how to fetch CCK fields value programmatically

The code below will fetch the field "field_featured", "field_traveldistance" for the node with vid. The fields can be different in your case

$field_array = array('field_featured', 'field_traveldistance');
foreach ($field_array as $field_name) {
 $field = content_fields($field_name);
 $db_info = content_database_info($field);
 $cck_field_values[$field_name] = db_result(
    db_query("SELECT %s FROM {%s} WHERE vid=%d", $db_info['columns']['value']['column'], $db_info['table'],
    $vid));
}

The array $cck_field_values contains CCK field values, this array can be used for theming search results. Hope this article was helpful. If you have any feedback get in touch with us using the comment form below.