最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - ACF plugin and field update

programmeradmin0浏览0评论

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 ?

Share Improve this question edited Oct 22, 2016 at 8:22 Fafanellu asked Oct 22, 2016 at 8:07 FafanelluFafanellu 2092 gold badges3 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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

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/

发布评论

评论列表(0)

  1. 暂无评论