【问题标题】:ACF object not returning dataACF 对象不返回数据
【发布时间】:2019-11-26 05:50:33
【问题描述】:

我正在创建一个自定义 rest api 端点以返回带有自定义字段的自定义帖子类型。

此刻我有这个:

function getPostVendaById( $request ) {
   // setup query argument
    $args = array(
        'p' => 4294,
        'post_type' => 'venda_de_casas',
    );

    // get posts
    $posts = get_posts( $args );

    // add custom field data to posts array 
    foreach ($posts as $key => $post) {
        $posts[$key]->acf = get_fields($post->ID);
        $posts[$key]->link = get_permalink($post->ID);
        $posts[$key]->image = 
        get_the_post_thumbnail_url($post->ID);
    }
    return $posts;
}

但这在我去后端并更新自定义帖子之前不起作用。

已经试过update_post ( $ID )还是不行,有什么帮助吗?

【问题讨论】:

  • 如果您在创建新帖子后立即运行此命令,则无法获取 ACF,因为创建帖子然后设置 post_meta 值需要一些时间,更新也是如此。你能解释一下你什么时候运行这个函数
  • @Beneris 请检查更新

标签: wordpress advanced-custom-fields wordpress-rest-api


【解决方案1】:

我建议不要为自定义帖子类型创建自定义端点,而是可以使用默认的 WordPress API 调用自定义帖子类型:

http://www.example.com/wp-json/wp/v2/<post_type_name>/

并为您的 WordPress 安装配置 ACF to Rest API 插件:https://github.com/airesvsg/acf-to-rest-api,它将您所有帖子类型的 acf 字段公开到 REST API 响应中。

注意:

在创建自定义帖子类型时保持以下选项为真:

'show_in_rest' => 是的

希望这会有所帮助!

【讨论】:

  • 不适合我,这意味着它不会作为单独的字段出现在 REST API 中
【解决方案2】:

更新:

我这样做是为了在我的自定义帖子上创建条目

$request = json_decode($request->get_body());
$post_id = wp_insert_post(array(
    'post_title'=> 'e',
    'post_type'=> 'venda_de_casas',
    'post_status'  => 'publish')
);

add_post_meta($post_id, 'nome', $request->name, true);
add_post_meta($post_id, 'email', $request->email, true);
add_post_meta($post_id, 'telemovel', $request->phone, true);
add_post_meta($post_id, 'tipo', $request->type, true);
add_post_meta($post_id, 'preco', $request->price[0].'€' . ' - ' . $request->price[1].'€', true);
add_post_meta($post_id, 'nº_quartos', $request->rooms, true);
add_post_meta($post_id, 'coordenadas', $request->coordinates->lat . ',' . $request->coordinates->lng, true);
add_post_meta($post_id, 'classe_energetica', $request->certificate, true);
add_post_meta($post_id, 'area_bruta', $request->maxArea, true);
add_post_meta($post_id, 'area_util', $request->utilArea, true);
add_post_meta($post_id, 'lugares_de_garagem', $request->garage, true);
add_post_meta($post_id, 'casas_de_banho', $request->bathrooms, true);
add_post_meta($post_id, 'distrito', $request->district->label, true);
add_post_meta($post_id, 'conselho', $request->concelho->label, true);
add_post_meta($post_id, 'freguesia', $request->freguesia->label, true);
add_post_meta($post_id, 'nº_quartos', $request->rooms, true);
add_post_meta($post_id, 'servicos_de_saude', $request->saude, true);
add_post_meta($post_id, 'zonas_verdes', $request->green, true);
add_post_meta($post_id, 'comercio', $request->comercio, true);
add_post_meta($post_id, 'escolas', $request->school, true);
add_post_meta($post_id, 'acessibilidade', $request->acessibilidade, true);
add_post_meta($post_id, 'cultura_e_lazer', $request->culture, true);

$images = array();

foreach($request->images as $image) {
    $image = uploadImageAndSave($post_id);
    array_push($images,$image);
    unlink($image);
}
update_field("field_5d2c5c78c5d0e", $images, $post_id );

$my_post = array(
    'post_type' => 'venda_de_casas',
    'ID' => $post_id ,
    'post_title' => 'e'
);

wp_update_post($my_post);

然后我为 getPostVendaById 创建了一个自定义 api 端点,以通过 id 获取自定义帖子类型,该调用是在我想要获取该特定帖子时进行的,而不是在创建之后进行

【讨论】:

  • 如果您尝试在插入后立即获取所有 acf,我认为您不需要运行 wp_update_post(),因为我提到这些元字段可能仍在 post_meta 表的路上,所以如果您立即需要该数据,从 $_REQUEST 使用它们,还尝试用 get_post_meta() 替换 get_fields() 我认为 ACF 函数是 set_field 和 get_field,如果您使用 add_post_meta() 添加它们,这可能会导致一些问题,如果这些 acf 不是使用 ACF 函数注册。
猜你喜欢
  • 2017-10-11
  • 2021-05-17
  • 2017-06-22
  • 2022-12-21
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
相关资源
最近更新 更多