自定义 WP 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' => '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');

这是我在函数中的代码.php用于注册自定义 REST API

    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 数据: http://localhost/wp-json/CustomAPI/v1/subscription/

[{"title":"Matt","region":"Central Visayas","country":"PH","activity":"2 months premium membership","time":"2018-01-31 06:36:53"},{"title":"Johnmark","region":"Central Visayas","country":"PH","activity":"Life time membership","time":"2018-01-31 06:29:52"}]

任何想法我的代码有什么问题,为什么它不显示给非登录用户?

将 'show_in_rest' 参数添加到register_post_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');

https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/

试试这段代码

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"参数。

最新更新