ACF Pro中的add_row不保存转发器值 [英] add_row in ACF Pro isn't saving repeater values

查看:81
本文介绍了ACF Pro中的add_row不保存转发器值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用高级自定义字段专业版在wordpress中存储帖子的元数据。

I'm using Advanced Custom Fields Pro to store metadata for a post in wordpress.

我的帖子是动态创建的(不是通过管理界面),这意味着我明确需要使用字段键来填充元数据,而不是字段名称。我的一个字段是具有单个文本区域的转发器字段,另一个是标准文本区域。

My posts are created dynamically (not through the administrative interface), which means that I explicitly need to populate metadata using field keys, not field names. One of my fields is a repeater field with a single text area and another is a standard text area.

以下代码是我所说的(每个帖子一次)。帖子是使用 wp_insert_post()创建的。

The following code is what I call (once per post). The post is created using wp_insert_post() earlier.

  // Populate "Name"
  update_field('field_566e360961c2f', 'John Doe', $wp_identifier);

  // Populate "Sponsors"
  foreach($sponsors as $sponsor) {

      // Define "Sponsor Name"
      $new_sponsor = array(
        'field_566e32fb943a5' => 'Beats and Corn Companye'
      );

      add_row('field_566e32bd943a4', $new_sponsor, $wp_identifier);
  }

其结果是填充了标准文本字段,并且有一个赞助商 已创建转发器项目,但发起人名称的值为空白。

The result of this is that standard text fields populate, and a single "sponsor" repeater item is created, but the value of the sponsor name is blank.

相关的 wp_postmeta 数据为生成的看起来像这样:

The relevant wp_postmeta data that is generated looks like this:

|   18226 |      71 | name                    | John Doe
|   19234 |      71 | sponsors                | 1                                                            |
|   19235 |      71 | _0_field_566e32fb943a5  | Beats and Corn Company                                                                             |



我的问题



我在做什么错误?查看 add_row() 这似乎是正确的方法。

My Question

What am I doing wrong? Looking at the documentation for add_row() this appears to be the correct approach. Is it possible that repeater fields have a different way of notating keys that I'm not aware of?

推荐答案

这不是可能吗?在今天的文档中已经非常清楚地说明了这一点,但是事实证明 add_row 仅在现有行已保存的情况下有效。第一次尝试填充中继器字段时,必须使用 update_field 并传递值数组数组。

This isn't made incredibly clear in the documentation today, but it turns out add_row only works if an existing row had already been saved. When trying to populate a repeater field for the very first time you have to use update_field instead and pass an array of value arrays.

  // Populate "Name"
  update_field('field_566e360961c2f', 'John Doe', $wp_identifier);

  // Populate "Sponsors"
  $new_sponsors = array();
  foreach($sponsors as $sponsor) {

      // Define "Sponsor Name"
      $new_sponsor = array(
        'field_566e32fb943a5' => 'Beats and Corn Companye'
      );
      $new_sponsors[] = $new_sponsor;
  }

  update_field('field_566e32bd943a4', $new_sponsors, $wp_identifier);

这篇关于ACF Pro中的add_row不保存转发器值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆