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 badges1 Answer
Reset to default 1You 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
版权声明:本文标题:customization - WordPress custom API endpoint - how to make the request more flexible 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742310123a2450698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论