问题描述
我正在为WP API编写自定义终结点,以从wordpress检索帖子,例如,这些帖子来自"real_estate" post_type,并且可容纳"5个或更多"人员.
I'm writing a custom endpoint for WP API, to retrieve posts from wordpress that for example are from the 'real_estate' post_type and capacity for '5 or more' persons.
我建立了一个新的自定义端点:
I've built a new custom endpoint:
// permite que meta_key e meta_value
// sejam filtrados pela api
function filtros( $valid_vars ) {
$valid_vars = array_merge(
$valid_vars,
array(
'meta_key',
'meta_value' ) );
return $valid_vars;
}
add_filter( 'rest_query_vars', 'filtros' );
// funcção que retorna posts do autor
function busca( $data ) {
$posts = get_posts(array(
'post_type' => 'imoveis',
'posts_per_page' => '1000',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'transacao',
'value' => $data['tipo']
),
array(
'key' => 'quartos',
'value' => $data['quartos'],
'compare' => '>'
)
)
));
if ( empty( $posts ) ) {
return new WP_Error( 'sem resultados', 'quartos: ' . $data['quartos'] . ' transacao: '. $data['tipo'], array( 'status' => 404 ) );
}
return $posts;
}
// cria o endpoint que ira receber a função acima
add_action( 'rest_api_init', function () {
register_rest_route( 'busca/v2', '/resultado/(?P<tipo>.+)/(?P<quartos>\d+)',
array(
'methods' => 'GET',
'callback' => 'busca',
)
);
});
搜索很好,正在运行,我按交易类型(出售或出租)和每个房地产中的房间数进行过滤.
The search is fine, it's working, i'm filtering by transaction type (sale or rent) and number of rooms in each real estate.
但是我的JSON响应缺少很多字段,包括ACF.前任:{
"ID":149,"post_author":"2","post_date":"2016-03-03 23:53:39","post_date_gmt":"2016-03-03 23:53:39","post_content":","post_title":"Ano的机会","post_excerpt":","post_status":发布","comment_status":已关闭","ping_status":关闭","post_password":","post_name":"oportunidade-do-ano","to_ping":","pinged":","post_modified":"2016-03-03 23:53:39","post_modified_gmt":"2016-03-03 23:53:39","post_content_filtered":","post_parent":0,"guid":"http://raphaelk.co/api/?post_type=imoveis&p=149","menu_order":0,"post_type":"imoveis","post_mime_type":","comment_count":"0","filter":"raw"}
But my JSON response is missing a lot of fields, including ACF.EX: {
"ID":149, "post_author":"2", "post_date":"2016-03-03 23:53:39", "post_date_gmt":"2016-03-03 23:53:39", "post_content":"", "post_title":"Oportunidade do Ano", "post_excerpt":"", "post_status":"publish", "comment_status":"closed", "ping_status":"closed", "post_password":"", "post_name":"oportunidade-do-ano", "to_ping":"", "pinged":"", "post_modified":"2016-03-03 23:53:39", "post_modified_gmt":"2016-03-03 23:53:39", "post_content_filtered":"", "post_parent":0, "guid":"http://raphaelk.co/api/?post_type=imoveis&p=149", "menu_order":0, "post_type":"imoveis", "post_mime_type":"", "comment_count":"0", "filter":"raw" },
你们有什么想法,我该如何更改该回复?并在其中添加ACF.
Do you guys have any idea how can i change that response? And include ACF to it.
谢谢
推荐答案
您尝试仅使用ACF函数 get_fields
吗?
Did you try to simply use the ACF function get_fields
?
在您的"busca"函数中,在 get_posts()
之后,如果 $ posts
不为空,则为每个帖子检索acf字段,如下所示:
In your "busca" function, after get_posts()
, if $posts
isn't empty, retrieve acf fields for each posts like this :
if ( empty( $posts ) ) {
return new WP_Error( 'sem resultados', 'quartos: ' . $data['quartos'] . ' transacao: '. $data['tipo'], array( 'status' => 404 ) );
} else {
foreach ($posts as $key => $post) {
$posts[$key]->acf = get_fields($post->ID);
}
}
希望能做到!
这篇关于将ACF添加到CUSTOM wp api端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!