如何在wordpress rest api中过滤自定义帖子类型的自定义字段?

问题描述 投票:0回答:1

我使用wordpress标准插件“高级自定义字段”和“custom_post_type ui”。我创建了一个名为deals的post_type,并添加了一些自定义字段。

我现在需要做的是,在访问其余api时过滤结果,如下所示:

http://localhost:8000/wp-json/wp/v2/deals

实际上我只需要它的acf部分。我不关心其余的事情。

[{"id":29,"date":"2019-04-12T12:34:14","date_gmt":"2019-04- 
12T12:34:14","guid":{"rendered":"http:\/\/localhost:8000\/? 
post_type=deals&p=29"},"modified":"2019-04- 
12T12:34:14","modified_gmt":"2019-04-12T12:34:14",
"slug":"test-title","status":"publish","type":"deals",
"link":"http:\/\/localhost:8000\/deal s\/test- title\/","template":"",
"meta":[],"tax-deals":[],"acf":{"title":"Title for Deals 
Post","description":"","image":false,"date_start":"01.01.1970",
"date_end":"01.01.1970","category":"Kleidung"},"_links":{"self": 
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/deals\/29"}],
"collection":[{"href":"http:\/\/localhost:8000\/wp- 
json\/wp\/v2\/deals"}],"about":[{"href":"http:\/\/localhost:8000\/wp- 
json\/wp\/v2\/types\/deals"}],"wp:attachment": 
[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media? 
parent=29"}],"wp:term":[{"taxonomy":"tax_deals","embeddable":true,
"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tax-deals? 
post=29"}],"curies": 
[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}},

我已经尝试过使用了

http://localhost:8000/wp-json/wp/v2/deals?search=id

获取id或者其他内容,但响应是空的。

这也没有用:

http://localhost:8000/wp-json/wp/v2/deals?id=28

再次空洞的回应。

总结一下:我需要通过我的响应json中显示的“acf”属性在我的自定义字段上过滤我的自定义帖子类型。它是如何工作的?

编辑:我已经安装了“WP REST过滤器”,但仍然不知道如何做到这一点。

wordpress rest
1个回答
1
投票

我建议你创建一个新的API,你可以自定义输出。利用wordpress功能register_rest_route(),您可以在一个ajax网址中创建CPT和ACF的API。而且您不需要安装任何东西。

检查我如何获得我的讲师CPT和mycheckbox ACF。

// your ajaxurl will be: http://localhost/yoursite/wp-json/custom/v2/instructor/
add_action( 'rest_api_init', function () {
  register_rest_route( 'custom/v2', '/instructor', array(
              'methods' => 'GET',
              'callback' => 'instructor_json_query',
  ));
});

// the callback function 
function instructor_json_query(){

   // args to get the instructor
   $args = array(
     'post_type' => 'instructor',
     'posts_per_page' => -1,
     'post_status' => 'publish',
     'meta_query' => array(
          array(
             'key' => 'mycheckbox', // your acf key
             'compare' => '=',
             'value' => '1' // your acf value
          )
      )
   );

 $posts = get_posts($args); 

 // check if $post is empty
 if ( empty( $posts ) ) {
     return null;
 }

 // Store data inside $ins_data 
 $ins_data = array();
 $i = 0;

 foreach ( $posts as $post ) {
    $ins_data[] = array(  // you can ad anything here and as many as you want
       'id' => $posts[$i]->ID,
       'slug' => $posts[$i]->post_name,
       'name' => $posts[$i]->post_title,
       'imgurl' => get_the_post_thumbnail_url( $posts[$i]->ID, 'medium' ),
    );
    $i++;
  }

  // Returned Data     
  return $ins_data;

 }

然后,您可以在您的ajax网址中使用链接:http://localhost/yoursite/wp-json/custom/v2/instructor/

© www.soinside.com 2019 - 2024. All rights reserved.