I want to update a field of node programmically,I did it with below code

$node = node_load($nid);
        $node->field_share_counter['und'][0]['value'] = intval($node->field_share_counter['und'][0]['value']) + 1;

but after update with this method the node will be update,I mean in content show that this node recently update, I want update custom field without the node updated.also I know db_update is suitable for update a field of table in db but in here I dont know how achieve this job.


3 Answers 3


I found this http://timonweb.com/how-insert-and-update-only-specific-fields-your-entity-drupal-7 and it is useful and final solution is :

$node->field_share_counter['und'][0]['value'] = intval($node->field_share_counter['und'][0]['value']) + 1;
        //$node->field_product_description[$node->language][0]['value'] = 'This is a very nice and useful product. Buy it now! We want your money!';

            // Get the numeric id of your field by passing field name
            $info = field_info_field('field_share_counter');
            $fields = array($info['id']);

            // Execute the storage function
            field_sql_storage_field_storage_write('node', $node, 'update', $fields);

            // Clear field cache
            cache_clear_all("field:node:$node->nid", 'cache_field');
  • Ah, interesting, I didn't know about field_sql_storage_field_storage_write() yet. That's definitely shorter than my solution!
    – berliner
    Commented Jan 29, 2014 at 12:18
  • @berliner how ever,I thank you for pay attention and spend time to answer question.
    – Yuseferi
    Commented Jan 29, 2014 at 12:35
  • 1
    You're welcome. As this example shows, I'm also learning by answering ;)
    – berliner
    Commented Jan 29, 2014 at 12:53
  • @berliner :) ;)
    – Yuseferi
    Commented Jan 29, 2014 at 13:06

I prefer using field_attach_presave('node', $node); and field_attach_update('node', $node); field API. Here is an example http://www.urbaninsight.com/2011/10/24/saving-nodes-fields-without-saving-node-itself. But it seems this API is not issued for this reason and has some consequences, fortunately it worked well every time for me.

  • I like this approach better than the sql_storage approach because it handles presave hooks and is not dependent on the storage engine used (though it's almost always sql_storage). Also note: that this will not quite work for all fields. For example, taxonomy term reference fields are saved properly but the taxonomy_index table is not updated with the field, but rather in node_update() and node_insert(). So even though the field updates, a term list page won't include the nodes tagged in this manner.
    – Chaulky
    Commented Jan 7, 2015 at 18:26

Completely untested code, but it should work around these lines:

// Load the field information.
$field_info = field_info_field('field_share_counter');
// Get the storage information, this assumes that the fields data is saved using a database backend.
$storage_info = $field_info['storage']['details']['sql'][FIELD_LOAD_CURRENT];
$tables = array_keys($storage_info);
// This is the table where the field data is stored.
$table = reset($tables);
$value_field = $storage_info[$table]['value'];
    $value_field => intval($node->field_share_counter[LANGUAGE_NONE][0]['value']) + 1,
  ->condition('entity_type', 'node')
  ->condition('bundle', $node->type)
  ->condition('entity_id', $node->nid)
  ->condition('language', LANGUAGE_NONE)
  ->condition('delta', 0)

This code does not respect revisions and it will come with possible side effects because other modules can not hook into this process. Also I'm in doubt about your motivation here. In general this is an error prone approach. Whenever possible you should let the Field API do the work for you. If you simply do not want users to know that the node has been updated, then should suppress this information from the user instead of circumventing default behavior by Drupal.

You can also have a look at this thread which describes how to do what you want using the Entity module.


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.