My posts have an ACF named curnumber, created with the ACF plugin. This ACF could be an issue number (for a magazine for instance). Let's assume the current issue is #788. Now I'd like to retrieve the latest issue in the database. I would use such a query :

$query = "
SELECT MAX(cast(meta_value AS unsigned)) 
FROM rkji_postmeta 
WHERE meta_key = 'curnumber'
$maxnum = $wpdb->get_var($query);

$maxnum would be : 788.

Let's say I made a mistake, and I now change my ACF in the post to 787. If I run the query again, the result would always be 788.

I looked at the database, and noticed that when a post is created, if it has a custom field, the postmeta table is udpated with 2 new records with 2 distinct meta-id s, while they share the same post_id. When the ACF changes, only one of those records change with the ACF set to the new value. Hence, the oldest value always remains in the db. Maybe it is due to the revision mechanism of Wordpress... Anyway, how can I do to handle this ? Maybe verify that the post is set to publish ?

1 Answer 1


From what I understand, you are trying to update an ACF field on a new post. From my experience, it is preferable to use the field key instead of the field name in the ACF functions - using the field name gets you in all sorts of strange situations - like the one you are having.

<?php update_field($selector, $value, $post_id); ?>
$selector (string) the field name or key (required)
$value (mixed) the value to save (required)
$post_id (mixed) Specific post ID where your value was entered.
 Defaults to current post ID (not required). 
 This can also be options / taxonomies / users / etc


In order to get the field key for your ACF fields, go to Custom Fields - your fieldset - Screen Options (top right) - Show Field Key = Yes

See this short tutorial about getting the field keys in ACF: http://thestizmedia.com/show-field-keys-acf-pro/

  • not on a new post, but on an existing post...
    – Fafanellu
    Commented Oct 24, 2016 at 7:57

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.