admin管理员组

文章数量:1334189

I'm creating simple API endpoint to fetch custom post type items with specified category and tags.

function my_awesome_func( $data ) {
  $posts = get_posts( array(
        'post_type' => 'faq',
        'numberposts' => 10,
        'tax_query' => array(
            'relation' => 'AND',
                array(
                        'taxonomy' => 'category',
                        'field' => 'slug',
                        'terms' => array($data['cat']) 
                ),
                array(
                    'taxonomy' => 'post_tag',
                        'field' => 'slug',
                        'terms' => array($data['tag']) 
                ),
            )
        )
  );
 
  if ( empty( $posts ) ) {
    return null;
    }

        
        foreach( $posts as $post ) {
            $id = $post->ID; 

            $posts_data[] = (object) array( 
                    'id' => $id, 
                    'title' => $post->post_title,
                    'content' => $post->post_content
            );
        }                  
    
    return $posts_data;
}

add_action( 'rest_api_init', function () {
  register_rest_route( 'test/v1', '/faq/cat=(?P<cat>[a-zA-Z0-9_,]+)/tag=(?P<tag>[a-zA-Z0-9_,]+)', array(
    'methods' => 'GET',
    'callback' => 'my_awesome_func',
  ) );
} );

It works fine with this request: ...wp-json/test/v1/faq/cat=cars/tag=big but I would like to make it more flexible, by adding only category, or only tags ...wp-json/test/v1/faq/cat=cars and by adding multiple tags (i.e separated by comma): ...wp-json/test/v1/faq/cat=cars/tag=big,blue

Thanks in advance for help.

I'm creating simple API endpoint to fetch custom post type items with specified category and tags.

function my_awesome_func( $data ) {
  $posts = get_posts( array(
        'post_type' => 'faq',
        'numberposts' => 10,
        'tax_query' => array(
            'relation' => 'AND',
                array(
                        'taxonomy' => 'category',
                        'field' => 'slug',
                        'terms' => array($data['cat']) 
                ),
                array(
                    'taxonomy' => 'post_tag',
                        'field' => 'slug',
                        'terms' => array($data['tag']) 
                ),
            )
        )
  );
 
  if ( empty( $posts ) ) {
    return null;
    }

        
        foreach( $posts as $post ) {
            $id = $post->ID; 

            $posts_data[] = (object) array( 
                    'id' => $id, 
                    'title' => $post->post_title,
                    'content' => $post->post_content
            );
        }                  
    
    return $posts_data;
}

add_action( 'rest_api_init', function () {
  register_rest_route( 'test/v1', '/faq/cat=(?P<cat>[a-zA-Z0-9_,]+)/tag=(?P<tag>[a-zA-Z0-9_,]+)', array(
    'methods' => 'GET',
    'callback' => 'my_awesome_func',
  ) );
} );

It works fine with this request: ...wp-json/test/v1/faq/cat=cars/tag=big but I would like to make it more flexible, by adding only category, or only tags ...wp-json/test/v1/faq/cat=cars and by adding multiple tags (i.e separated by comma): ...wp-json/test/v1/faq/cat=cars/tag=big,blue

Thanks in advance for help.

Share Improve this question asked Jun 25, 2020 at 10:52 cooper33cooper33 155 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You need to register separate routes for each scenario you want to cover (ie, cat & tag, just cat, or just tag), all done in the single add_action call. The regex you have will support the comma separated tags.

The next thing to do is change how you set the 'term' parameter as part of the 'tax_query' array parameter. It's expecting an array of strings, currently you are passing a single string of comma separated values (ie, 'tag1,tag2'). You can pass the result of using explode() to convert the string to an array.

You will also need to implement some conditional logic around setting the 'tax_query' array parameter based on what route is used.

本文标签: customizationWordPress custom API endpointhow to make the request more flexible