Klaxx
Klaxx

Reputation: 400

How to filter custom fields for custom post type in wordpress rest api?

I use wordpress standard with the plugins "Advanced custom fields" and "custom_post_type ui". I created a post_type called deals and added some custom fields with it.

What I need to do now, is filter the results when accessing the rest api like this:

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

Actually I only need the acf part of it. I dont care about the rest.

[{"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}]}},

I have already tried using

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

to get the id or something, but response is empty.

Also this didnt work:

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

Again empty response.

To summarize: I need to filter my custom post type on my custom fields by the "acf" attribute shown in my response json. How does it work?

EDIT: I already installed "WP REST Filter" but still dont know how to do it.

Upvotes: 1

Views: 2866

Answers (1)

Mel
Mel

Reputation: 943

I suggest you to create a new API where you can customize the output. Take advantage of wordpress function register_rest_route() using this you can create an API from CPT and ACF in one ajax url. And you do not need to install anything.

Check how I get my instructor CPT and 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;

 }

Then, you can use the link: http://localhost/yoursite/wp-json/custom/v2/instructor/ in your ajax url.

Upvotes: 1

Related Questions