admin管理员组文章数量:1391974
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
?
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
Reset to default 0From 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
https://www.advancedcustomfields/resources/update_field/
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/show-field-keys-acf-pro/
本文标签: post metaACF plugin and field update
版权声明:本文标题:post meta - ACF plugin and field update 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684514a2619616.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论