Wordpress 未为未登录用户显示自定义WP REST API
我有他的自定义邮件类型Wordpress 未为未登录用户显示自定义WP REST API,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我有他的自定义邮件类型 function wp_post_types() { // Nicknames post type register_post_type('subscription', array( 'supports' => array('title'), 'public' => true, 'show_ui' => true, 'labels' => array( 'name' => 'Subs
function wp_post_types() {
// Nicknames post type
register_post_type('subscription', array(
'supports' => array('title'),
'public' => true,
'show_ui' => true,
'labels' => array(
'name' => 'Subscription',
'add_new_item' => 'Add New Subscription',
'edit_item' => 'Edit Subscriptions',
'all_items' => 'All Subscriptions',
'singular_name' => 'Subscription'
),
'menu_icon' => 'dashicons-universal-access-alt'
));
}
add_action('init', 'wp_post_types');
下面是我在function.php中注册自定义RESTAPI的代码
add_action('rest_api_init', 'searchSubscription');
function searchSubscription() {
register_rest_route('CustomAPI/v3', 'search', array(
'methods' => WP_REST_SERVER::READABLE,
'callback' => 'getSubscription'
));
}
function getSubscription() {
$subscriptions = new WP_Query(array(
'perm' => 'readable',
'post_type' => 'subscription',
'posts_per_page' => '5'
));
$subscriptionResults = array();
while($subscriptions->have_posts()) {
$subscriptions->the_post();
array_push($subscriptionResults, array(
'title' => get_the_title(),
'region' => get_field('regi'),
'country' => get_field('country'),
'activity' => get_field('activity'),
'time' => get_field('time')
));
}
return $subscriptionResults;
}
问题:它不会为未登录的用户显示任何结果。错误:
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
它适用于管理员用户。我可以从以下位置查看json数据:
你知道我的代码有什么问题吗?为什么没有为未登录的用户显示?试试这段代码
add_action('rest_api_init', 'searchSubscription');
function searchSubscription() {
register_rest_route('CustomAPI/v1', 'subscription', array(
'methods' => WP_REST_SERVER::READABLE,
'callback' => 'getSubscription'
));
}
function getSubscription() {
$subscriptions = new WP_Query(array(
'perm' => 'readable',
'post_type' => 'subscription',
'posts_per_page' => '5'
));
$subscriptionResults = array();
while($subscriptions->have_posts()) {
$subscriptions->the_post();
array_push($subscriptionResults, array(
'title' => get_the_title(),
'region' => get_field('regi'),
'country' => get_field('country'),
'activity' => get_field('activity'),
'time' => get_field('time')
));
}
return $subscriptionResults;
}
尝试删除“perm”参数。将“show\u in\u rest”参数添加到
register\u post\u type()函数中
function wp_post_types() {
// Nicknames post type
register_post_type('subscription', array(
'supports' => array('title'),
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'labels' => array(
'name' => 'Subscription',
'add_new_item' => 'Add New Subscription',
'edit_item' => 'Edit Subscriptions',
'all_items' => 'All Subscriptions',
'singular_name' => 'Subscription'
),
'menu_icon' => 'dashicons-universal-access-alt'
));
}
add_action('init', 'wp_post_types');
我的第一个想法是将post类型设置为publictrue@Stender是的,register_post_type现在使用'public'=>true,对于未登录用户仍然没有显示。我将'CustomAPI/v1'、'/search/'更改为'CustomAPI/v1'、'/search/'但对于未登录用户仍然没有结果。管理员可以查看json数据
function wp_post_types() {
// Nicknames post type
register_post_type('subscription', array(
'supports' => array('title'),
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'labels' => array(
'name' => 'Subscription',
'add_new_item' => 'Add New Subscription',
'edit_item' => 'Edit Subscriptions',
'all_items' => 'All Subscriptions',
'singular_name' => 'Subscription'
),
'menu_icon' => 'dashicons-universal-access-alt'
));
}
add_action('init', 'wp_post_types');