简体   繁体   English

WordPress高级自定义字段(ACF)未保存

[英]WordPress Advanced Custom Fields (ACF) not saved

I have a WordPress website which uses ACF (Advanced Custom Fields). 我有一个使用ACF(高级自定义字段)的WordPress网站。 I have 2 custom select fields which allow a user to be selected on posts - these fields are called 'Author' and 'Editor'. 我有2个自定义选择字段,允许在帖子上选择用户-这些字段称为“作者”和“编辑者”。

The site has around 5000 posts and, for some of the older posts on the site, the custom 'Author' and 'Editor' fields are not being pulled through to the frontend of the site even though the custom fields appear to be set correctly within the post editor screen. 该网站大约有5000个帖子,对于该网站上的一些较早的帖子,自定义的“作者”和“编辑”字段不会被拉到网站的前端,即使自定义字段似乎在内部正确设置帖子编辑器屏幕。

If I go in to an individual post and re-save/update it, then the custom fields appear to then work fine and are pulled through to the frontend of the site. 如果我进入单个帖子并重新保存/更新它,那么自定义字段似乎可以正常工作,并被拉到站点的前端。 Unfortunately, bulk editing the posts and re-saving/updating them doesn't appear to have the same effect of fixing the posts. 不幸的是,批量编辑帖子并重新保存/更新它们似乎对修复帖子没有相同的效果。 As I have 5000 posts it's not really viable to re-save/update each one individually. 由于我有5000个帖子,因此单独重新保存/更新每个帖子真的不可行。

Update: I had already tried the instructions in the link provided in the comments from Stender - it doesn't appear to update the ACF fields data in the same way as manually updating a single post. 更新:我已经尝试过Stender注释中提供的链接中的说明-似乎没有以与手动更新单个帖子相同的方式更新ACF字段数据。 Does something different happen during the manual method as opposed to using wp_update_post()? 与使用wp_update_post()相比,手动方法会发生什么不同的事情吗?

Does anyone have any ideas on how I can fix the posts where the custom fields aren't being pulled through to the frontend in bulk? 有没有人对如何解决自定义字段无法批量发送到前端的帖子有任何想法?

If you need any more information or have any questions, please feel free to ask. 如果您需要更多信息或有任何疑问,请随时提问。

So, it turns out that the ACF custom field data is actually stored correctly already, but for some reason the ACF function get_field() isn't pulling in the data for certain posts when it is definitely there. 因此,事实证明ACF自定义字段数据实际上已经正确存储了,但是由于某些原因,ACF函数get_field()确实存在时并没有为某些帖子提取数据。 It appears to be old posts that are affected only and I'm not sure of the exact cause. 看来是只受到影响的旧帖子,我不确定确切原因。 (Older posts may have been imported, but I'm not 100% sure) (较早的帖子可能已导入,但我不确定100%)

Anyway, I got around the problem in the end by using WordPress' own function for retrieving meta data/custom fields: get_post_meta() . 无论如何,我最终使用WordPress自己的函数检索元数据/自定义字段( get_post_meta()来解决该问题。 There's a bit more code involved but it now works for me. 涉及到更多代码,但现在对我有用。

Thanks for everyone's efforts in helping anyway! 感谢大家的努力!

I found a blog post with instructions on how to bulk-update ACF (post_meta) fields. 我找到了一篇博客文章,其中包含有关如何批量更新ACF(post_meta)字段的说明。

function mass_update_posts() {

    $args = array(
        'post_type'=>'post-type',
        'posts_per_page'   => -1
    );

    $my_posts = get_posts($args);

    foreach($my_posts as $key => $my_post) {
        $meta_values = get_post_meta( $my_post->ID);

        foreach($meta_values as $meta_key => $meta_value) {
            update_field($meta_key, $meta_value[0], $my_post->ID);
        }
    }
}

You can call this function by hooking into init . 您可以通过挂钩init来调用此函数。

add_action( 'init', 'mass_update_posts' );

I would add the action and function, load the website once, and then comment it out so it doesn't load again. 我将添加操作和功能,一次加载该网站,然后将其注释掉,这样就不会再次加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM