$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Unable to get the info of the user which doesn't have created any post via REST API|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Unable to get the info of the user which doesn't have created any post via REST API

matteradmin8PV0评论

I am unable to get the user info from the REST API. The user has not any post created. I have installed the Basic-Auth plugin. Whenever I try to get the user info, I get the following error.

Request :

{
    "code": "rest_user_cannot_view",
    "message": "Sorry, you are not allowed to list users.",
    "data": {
        "status": 401
    }
}

But whenever I request the other user, I get the info.

{
    "id": 1,
    "name": "asdf",
    "url": "",
    "description": "",
    "link": "/",
    "slug": "asdf",
    "avatar_urls": {
        "24": ";d=mm&r=g",
        "48": ";d=mm&r=g",
        "96": ";d=mm&r=g"
    },
    "meta": [],
    "_links": {
        "self": [
            {
                "href": ""
            }
        ],
        "collection": [
            {
                "href": ""
            }
        ]
    }
}

I need to get the user info, even if the user have not created any posts.

I am unable to get the user info from the REST API. The user has not any post created. I have installed the Basic-Auth plugin. Whenever I try to get the user info, I get the following error.

Request : https://example/wp-json/wp/v2/users/6

{
    "code": "rest_user_cannot_view",
    "message": "Sorry, you are not allowed to list users.",
    "data": {
        "status": 401
    }
}

But whenever I request the other user, I get the info.

{
    "id": 1,
    "name": "asdf",
    "url": "",
    "description": "",
    "link": "https://example/author/drzvikrant/",
    "slug": "asdf",
    "avatar_urls": {
        "24": "https://secure.gravatar/avatar/4bc329d2835e6584d5b492fdc67a5cde?s=24&d=mm&r=g",
        "48": "https://secure.gravatar/avatar/4bc329d2835e6584d5b492fdc67a5cde?s=48&d=mm&r=g",
        "96": "https://secure.gravatar/avatar/4bc329d2835e6584d5b492fdc67a5cde?s=96&d=mm&r=g"
    },
    "meta": [],
    "_links": {
        "self": [
            {
                "href": "https://example/wp-json/wp/v2/users/1"
            }
        ],
        "collection": [
            {
                "href": "https://example/wp-json/wp/v2/users"
            }
        ]
    }
}

I need to get the user info, even if the user have not created any posts.

Share Improve this question asked Mar 8, 2019 at 5:30 Sagar Bahadur TamangSagar Bahadur Tamang 1331 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

This is by design and within WP_REST_Users_Controller::get_item_permissions_check() we e.g. have a non-zero check with count_user_posts( $user->ID, $types ).

We note that count_user_posts() is filterable with the get_usernumposts filter so one could change 0 to 1, for the /wp/v2/users/\d+ route:

add_filter( 'rest_request_before_callbacks', function( $response, $handler, $request ){

    if ( WP_REST_Server::READABLE !== $request->get_method() ) {
        return $response;
    }

    if ( ! preg_match( '~/wp/v2/users/\d+~', $request->get_route() ) ) {
        return $response;
    }

    add_filter( 'get_usernumposts', function( $count ) {
        return $count > 0 ? $count : 1;
    } );

    return $response;
}, 10, 3 );

but I would rather recommend creating a custom rest route instead of modifying an existing one like this.

Post a comment

comment list (0)

  1. No comments so far