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