如何在Wordpress Rest API中为自定义帖子类型过滤自定义字段?

时间:2019-04-12 14:15:42

标签: wordpress rest

我将wordpress standard与“高级自定义字段”和“ 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过滤器”,但仍然不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

我建议您创建一个新的API,您可以在其中自定义输出。利用wordpress函数register_rest_route()的优势,您可以在一个ajax URL中从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/