ACF对象不返回数据

时间:2019-07-17 11:47:21

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

我正在创建一个自定义的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 )仍然无法正常工作,有帮助吗?

2 个答案:

答案 0 :(得分:0)

更新:

让我在我的自定义帖子上创建条目

$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);

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

答案 1 :(得分:0)

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

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

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

注意:

在创建自定义帖子类型时,使以下选项为true:

  

'show_in_rest'=>是

希望这会有所帮助!