简体   繁体   English

WordPress:无法在wp_insert_post之后设置帖子的ACF转发器字段

[英]Wordpress: Cannot set ACF repeater field of a post after wp_insert_post

I'm now using WordPress Advanced Custom Fields Plugin: http://www.advancedcustomfields.com/ 我现在正在使用WordPress高级自定义字段插件: http : //www.advancedcustomfields.com/

And I set a repeater field rows on my custom post_type sales_order : 然后在自定义的post_type sales_order上设置中继器字段rows

Then, now insert a post using script: 然后,现在使用脚本插入帖子:

$post_id = wp_insert_post(array(
    'post_title' => $title,
    'post_name' => $slug,
    'post_content' => $content,
    'post_type' => 'sales_order',
    'post_status' => 'publish',
));

But when I do: 但是当我这样做时:

update_field('rows', array(), $post_id);

It has no effect. 没有作用。

But if I manually save the post in the admin panel first, then call the update_field method, it works. 但是,如果我先手动将帖子保存在管理面板中,然后调用update_field方法,则它可以工作。


So, I tried to spy the wp_postmeta database table, I found that if I call wp_insert_post using script, that post don't generate the meta 因此,我尝试监视wp_postmeta数据库表,发现如果我使用脚本调用wp_insert_post ,则该帖子不会生成元

| meta_key | meta_value          |
|----------|---------------------|
| _rows    | field_568e7aeb22714 |

correctly. 正确地。

But I have to do the import workflow using pure script, how can I work around this? 但是我必须使用纯脚本来执行导入工作流,如何解决这个问题?

You are passing field_name but update_field requires field_key as the documentation says update_field 您正在传递field_nameupdate_field需要field_key如文档所述update_field

Consider this example 考虑这个例子

update_field('field_568e7aeb22714', array(), $post_id);

Finally I made a solution according the source. 最后,我根据来源提出了解决方案。


Reason: 原因:

Why the below action fails? 为什么以下操作失败?

update_field('rows', array(), $post_id);

Now we're using the field name , not the field key . 现在我们使用的是字段名 ,而不是字段键

In this case, ACF would do the follows: 在这种情况下,ACF将执行以下操作:

  1. Try to find the current post meta which has a meta_key : _rows , which prepend an under-slash before the field name . 尝试找到具有meta_key_rows的当前帖子元,该元后在字段名之前加一个反斜杠。
  2. After that, get that meta_value , it should be a field key which has a form like field_XXXXXXX 之后,获取该meta_value ,它应该是具有像field_XXXXXXX这样的格式的字段键
  3. Update the field with the founded field_key . 使用已建立的field_key更新该字段。

Now we know, if we do wp_insert_post in such a way, the _rows meta did not generate the correct meta_value . 现在我们知道,如果我们以这种方式执行wp_insert_post ,则_rows meta不会生成正确的meta_value

So if we update the post in the admin panel, the field key was then submitted and generating the correct meta_value . 因此,如果我们在管理面板中更新帖子,则将提交字段键并生成正确的meta_value

Conclusion: Only THAT meta value matters. 结论: 只有 meta值的问题。


Solution: 解:

So, in fact, the only things we should do more is to set that meta_values defined in the ACF field set bound to the specified post_type . 因此,实际上,我们要做的唯一的事情就是设置在ACF字段集中定义的meta_values绑定到指定的post_type

The below routine worked that around: 下面的例程可以解决此问题:

// The current post_type to search
$post_type = 'sales_order';

$post_id = wp_insert_post(array(
    'post_title' => $title,
    'post_name' => $slug,
    'post_content' => $content,
    'post_type' => $post_type,
    'post_status' => 'publish',
));

foreach (get_posts(array('post_type' => 'acf', 'posts_per_page' => -1)) as $acf) {
    $meta = get_post_meta($acf->ID);
    $rule = unserialize($meta['rule'][0]);
    if($rule['param'] == 'post_type' &&
        $rule['operator'] == '==' &&
        $rule['value'] == $post_type) {
        foreach($meta as $key => $field) {
            if(substr($key, 0, 6) == 'field_') {
                $field = unserialize($field[0]);
                update_post_meta($post_id, '_'.$field['name'], $key);
            }
        }
    }
}

Note that, we loop through all defined ACF posts, and check for the rule meta, to confirm if it was bounded on the specified post_type . 注意,我们遍历所有定义的ACF帖子,并检查rule meta,以确认它是否限制在指定的post_type

If so, loop all that ACF meta starts with field_ , and set the meta on the current post. 如果是这样,循环所有ACF元数据以field_开头,并在当前帖子上设置元数据。

All done. 全部做完。

$i = 0; //for metakey
$j=0; //files Count

foreach($files as $fileid){
    $j++;
    $meta_key = 'files_'.$i.'_file';
    //"files" repeater main name and "file" subname ;
    update_post_meta($post_id,$meta_key,$fileid);
    $i++;

}
update_post_meta($post_id,'files',$j);

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

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