Skip to content

Post Author Invalid #52

Open
Open

Description

@rulim34

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

Activity

  1. alanv73 commented on Oct 28, 2020

    @alanv73

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions