Co-Authors-Plus icon indicating copy to clipboard operation
Co-Authors-Plus copied to clipboard

get_guest_author_post_count never returns zero

Open jonathanstegall opened this issue 4 years ago • 14 comments

Recently I've noticed that when I have a guest author that has no posts, they still show up as having at least one. This happens both in the admin table and also on the front end when I use get_guest_author_post_count. But then when I click the Posts column, it goes to an admin page with no results.

Example:

image image

I've done a little bit of error logging on this function:

public function get_guest_author_post_count( $guest_author ) {
    if ( ! is_object( $guest_author ) ) {
        return 0;
    }

    $term       = $this->get_author_term( $guest_author );
    $guest_term = get_term_by( 'slug', 'cap-' . $guest_author->user_nicename, $this->coauthor_taxonomy );

    if ( is_object( $guest_term )
        && ! empty( $guest_author->linked_account )
        && $guest_term->count ) {
        $user = get_user_by( 'login', $guest_author->linked_account );
        if ( is_object( $user ) ) {
            error_log( 'count for id ' . $user->ID . ' is ' . count_user_posts( $user->ID, 'post', true ) );
            return count_user_posts( $user->ID, 'post', true );
        }
    } elseif ( $term ) {
        error_log( 'term count is ' . $term->count );
        return $term->count;
    }

    return 0;
}

So even when I force post on the post type, and public on the public value, it still returns at least one for every author.

Is this a WordPress bug, a co-authors bug, or expected behavior? That is, if I want to find an author who has zero posts, should I be looking for an author that has one post or less?

jonathanstegall avatar Jul 09 '20 17:07 jonathanstegall

I don't see this happening. I simply created a new guest author, and I see 0 posts for him. Any further steps to reproduce?

Screenshot from 2020-09-25 03-57-00

TheCrowned avatar Sep 25 '20 01:09 TheCrowned

I'm not sure, really. The term object itself comes through with the count value.

[25-Sep-2020 15:46:26 UTC] term is WP_Term Object
(
    [term_id] => 95336
    [name] => this-user-name
    [slug] => cap-this-user-name
    [term_group] => 0
    [term_taxonomy_id] => 266607
    [taxonomy] => author
    [description] => [desc info]
    [parent] => 0
    [count] => 1
    [filter] => raw
)

Do I need to do more investigating into what query WP core is running and what parameters it's passing, maybe?

jonathanstegall avatar Sep 25 '20 15:09 jonathanstegall

Is that guest author linked to a proper WP user? Can you reproduce the issue with a new guest author?

TheCrowned avatar Sep 27 '20 18:09 TheCrowned

It does still happen with a new guest author. It doesn't seem to matter if they are linked to a proper WP user or not. It shows the same both ways.

jonathanstegall avatar Sep 28 '20 13:09 jonathanstegall

Can you confirm it happens even with the latest commit from master? What WP version are you running?

TheCrowned avatar Sep 28 '20 14:09 TheCrowned

I'm running 5.5.1 of WP Core, and yes I'm running the latest commit from master. I did a fresh clone just now and there are no differences.

jonathanstegall avatar Sep 28 '20 14:09 jonathanstegall

I guess we'll have to wait for somebody else's input - I seem just unable to reproduce the issue! :(

TheCrowned avatar Oct 03 '20 07:10 TheCrowned

I could reproduce this problem with a plain WP 5.7 installation using the latest CAP version from this repo. Strange enough, even with no posts available, the two users that I created editor and author show a post count of 1. When deactivating the CAP plugin, the post count then shows 0 as expected.

post-count

nielslange avatar Mar 26 '21 03:03 nielslange

After running wp co-authors-plus update-author-terms, the problem disappeared and the correct count showed up. When creating a brand new site, I could no longer reproduce this problem.

@jonathanstegall Are you able to run wp co-authors-plus update-author-terms on your end to see if that corrects the post count?

nielslange avatar Mar 26 '21 04:03 nielslange

@jonathanstegall Do you still face the same problem with the latest version of the plugin?

nielslange avatar Mar 29 '21 12:03 nielslange

Sorry, I missed that there were tags for me in this issue. I'll do some testing as soon as possible, and make sure I'm using the updated version/check to see if I still see the issue.

jonathanstegall avatar Apr 17 '21 00:04 jonathanstegall

@nielslange I do still have this issue on the most recent version of the plugin. I cloned the master branch, and the existing authors where I've seen this issue are still having it. I also created a new author, with no posts, and that author still reports that they have one post.

jonathanstegall avatar Apr 17 '21 00:04 jonathanstegall

@jonathanstegall Do you mean, you checked out the latest version of the plugin and then created a new author account. Once you created that author account, it automatically shows a post count of 1? If that's not the case, can you please let me know how to reproduce the problem that you are facing?

nielslange avatar Apr 19 '21 02:04 nielslange

@nielslange yes, that's right. I checked out the latest version and created a new author account, which immediately shows a post count of 1 even though it does not have any posts.

jonathanstegall avatar Apr 19 '21 02:04 jonathanstegall