Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Author Invalid #52

Open
rulim34 opened this issue Jul 14, 2020 · 1 comment
Open

Post Author Invalid #52

rulim34 opened this issue Jul 14, 2020 · 1 comment

Comments

@rulim34
Copy link

rulim34 commented Jul 14, 2020

I just followed the tutorial, using my own WordPress site url. But some of the post author is invalid. The author name that appears instead is another author on my site

@alanv73
Copy link

alanv73 commented Oct 28, 2020

I'm finding similar behavior. Posts show with an incorrect author. The most recent post appears to be correct, not sure if it's merely by chance or what, but the rest of them are incorrect.

Edit: I think I see why this is happening.

In your fetchUser method, which is called by the _postBuilder method in the fetchPosts method, you are using the search api endpoint and it is often finding multiple users (if I search for id# 7, I find user with the id=7 and myself, which has a 7 in the username) but only returning the first result, which is often the incorrect result. A better method would be to query the users endpoint (/wp/v2/users/) instead.

String endpoint = '';

    if (id != null) {
      endpoint = '/$id'; //params['search'] = '$id';
    } else if (email != null)
      params['search'] = email;
    else if (username != null) params['search'] = username;

    endpoint += constructUrlParams(params);

    url.write(endpoint);

    final response = await http.get(url.toString(), headers: _urlHeader);

    if (response.statusCode >= 200 && response.statusCode < 300) {
      final jsonStr = json.decode(response.body);
      if (jsonStr.length == 0)
        throw new WordPressError(
            code: 'wp_empty_list', message: "No users found");

      return User.fromJson(jsonStr is List ? jsonStr[0] : jsonStr);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants