admin管理员组文章数量:1320917
I have attempted to register the route, tried all sorts of different parameters, feel my code looks like many of the examples on this site, but can't figure out why it doesn't work.
//Register REST Route
add_action('rest_api_init', 'add_advent_api');
function add_advent_api()
{
register_rest_route('adventapi/v1', '/adventapi/', array(
'methods' => 'WP_REST_Server::READABLE',
'callback' => function () {
return 'hello';
},
'args' => array(
'adventyear' => array(
'required' => true,
'type' => 'integer',
'description' => 'Year required',
'minimum' => 1972,
'maximum' => 9999,
),
'adventday' => array(
'required' => true,
'type' => 'integer',
'minimum' => 1,
'maximum' => 24,
),
),
'permission_callback' => function () {
return true;
}
));
}
function advent_query(WP_REST_Request $request)
{
return 'i work';
/* $adventyear = $_GET['adventyear'];
$adventday = $_GET['adventday'];
$args = array(
'post_type' => 'advent',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'advent_year',
'value' => $adventyear,
),
array(
'key' => 'advent_day',
'value' => $adventday,
),
),
);
$query = new WP_Query($args);
if ($query->have_posts()) {
echo 'i ahve posts';
} else {
echo 'No Results';
} */
}
I have attempted to register the route, tried all sorts of different parameters, feel my code looks like many of the examples on this site, but can't figure out why it doesn't work.
//Register REST Route
add_action('rest_api_init', 'add_advent_api');
function add_advent_api()
{
register_rest_route('adventapi/v1', '/adventapi/', array(
'methods' => 'WP_REST_Server::READABLE',
'callback' => function () {
return 'hello';
},
'args' => array(
'adventyear' => array(
'required' => true,
'type' => 'integer',
'description' => 'Year required',
'minimum' => 1972,
'maximum' => 9999,
),
'adventday' => array(
'required' => true,
'type' => 'integer',
'minimum' => 1,
'maximum' => 24,
),
),
'permission_callback' => function () {
return true;
}
));
}
function advent_query(WP_REST_Request $request)
{
return 'i work';
/* $adventyear = $_GET['adventyear'];
$adventday = $_GET['adventday'];
$args = array(
'post_type' => 'advent',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'advent_year',
'value' => $adventyear,
),
array(
'key' => 'advent_day',
'value' => $adventday,
),
),
);
$query = new WP_Query($args);
if ($query->have_posts()) {
echo 'i ahve posts';
} else {
echo 'No Results';
} */
}
Share
Improve this question
asked Nov 10, 2020 at 1:07
Kyle DunlopKyle Dunlop
133 bronze badges
2
- Did you flush your permalinks? – Tom J Nowell ♦ Commented Nov 10, 2020 at 1:57
- Yes, I have tried that. – Kyle Dunlop Commented Nov 10, 2020 at 2:34
1 Answer
Reset to default 0There's an issue in your code: The HTTP method is not valid because WP_REST_Server::READABLE
is a class constant, hence it should not be wrapped in quotes:
register_rest_route( 'adventapi/v1', '/adventapi/', array(
// 'methods' => 'WP_REST_Server::READABLE', // wrong
// 'methods' => "WP_REST_Server::READABLE", // wrong
'methods' => WP_REST_Server::READABLE, // correct
// ... other args.
) );
You should also ensure that you use the correct request/HTTP method as well as correct endpoint URL, e.g. https://example/wp-json/adventapi/v1/adventapi
, when making requests to the API.
本文标签:
版权声明:本文标题:Creating a custom endpoint for rest, I see the endpoint exists in the wp-json, but the request is returning 404 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742005604a2411892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论