Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#27998 closed defect (bug) (fixed)

the_author_posts() returns an inaccurate value with the author has no published posts

Reported by: jbenton's profile jbenton Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.0 Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

When the_author_posts() is run for a user who has zero published posts, the value returned is the total number of published posts on the entire WP install. First noticed when I saw someone with 0 posts listed as having 4,181 — the total post count for the site. Confirmed on another install that had 30 total posts and returned 30 for a user with zero posts.

Details, with confirmation from @nacin, here (see the thread) — apparently this was broken in 3.0:

https://twitter.com/jbenton/status/459083084189929472

Attachments (3)

27998.diff (496 bytes) - added by krogsgard 11 years ago.
Perform a check on get_post() within get_the_author_posts() to avoid a notice.
27998.2.diff (489 bytes) - added by krogsgard 11 years ago.
Use ! instead of empty() on the check for get_post().
27998-unit-tests.diff (687 bytes) - added by aaroncampbell 11 years ago.

Download all attachments as: .zip

Change History (11)

#1 @nacin
11 years ago

  • Version changed from 3.8.3 to 3.0

This happens because:

  • on an author page for someone with no posts (which is a proper 200 page, not a 404), $post is null
  • get_the_author_posts() uses get_post()->post_author directly
  • count_user_posts() uses get_posts_by_author_sql() to build a WHERE
  • get_posts_by_author_sql() doesn't put a condition on post_author if an actual author wasn't passed

We added count_user_posts() in 3.0, replacing the then-direct SQL in get_usernumposts().

#2 @krogsgard
11 years ago

It seems to me that two things may need to occur:

  • Do a check for a returned value on get_post()->post_author in get_the_author_posts() to prevent a notice.
  • Create the condition to exit early from get_posts_by_author_sql if a user ID isn't passed. Is there any reason to create continue without a post author?

I'd love for this to be my first patch, but want to make sure I go the right route :)

@krogsgard
11 years ago

Perform a check on get_post() within get_the_author_posts() to avoid a notice.

#3 @nacin
11 years ago

  • Keywords needs-unit-tests added

This looks good. There is need for empty(), it can just be ! $post, as the variable will always exist.

I don't think we need to venture further down the stack.

This will also need unit tests.

@krogsgard
11 years ago

Use ! instead of empty() on the check for get_post().

#4 @aaroncampbell
11 years ago

  • Keywords needs-unit-tests removed

27998-unit-tests.diff adds one test with two assertions. One with no global post set up and one with. The first errors before the patch and passes after. The second passes before and after.

#5 @aaroncampbell
11 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to Future Release

#6 @SergeyBiryukov
11 years ago

  • Component changed from Users to Post Thumbnails
  • Milestone changed from Future Release to 4.0

#7 @SergeyBiryukov
11 years ago

  • Component changed from Post Thumbnails to Posts, Post Types

#8 @wonderboymusic
11 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 28362:

In get_the_author_posts(), if there is no current $post, return 0 and bail.

Props krogsgard, aaroncampbell.
Fixes #27998.

Note: See TracTickets for help on using tickets.