WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#35512 closed defect (bug) (fixed)

Inconsistent behavior when passing `post_status=any` to WP_Comment_Query

Reported by: danielbachhuber Owned by: boonebgorges
Milestone: 4.5 Priority: normal
Severity: normal Version:
Component: Comments Keywords: has-patch has-unit-tests 2nd-opinion
Focuses: Cc:

Description

WP_Query has magical behavior for post_status=any, where it expands to any post status. WP_Comment_Query treats post_status=any literally, creating join SQL where post_status='any'.

I'd expect WP_Comment_Query to exhibit the same behavior as WP_Query (with post_type=any too)

Attachments (4)

35512.patch (6.4 KB) - added by kouratoras 4 years ago.
Patch file for 35512
35512.2.patch (6.3 KB) - added by kouratoras 4 years ago.
Modified patch
35512.diff (6.1 KB) - added by boonebgorges 4 years ago.
35512.3.patch (6.0 KB) - added by kouratoras 4 years ago.

Download all attachments as: .zip

Change History (10)

#1 @kouratoras
4 years ago

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

I attach a patch file with implementation of this functionality.

It also includes unit tests that cover all possible cases.

During the unit testing, I found an issue in another test function for the related ticket #20006 and submitted a patch for this on #35633.

@kouratoras
4 years ago

Patch file for 35512

#2 @boonebgorges
4 years ago

  • Keywords 2nd-opinion added
  • Milestone changed from Future Release to 4.5

Thanks for the patch, @kouratoras.

Currently, omitting the 'post_type' parameter will result in post_type being ignored altogether. That is, when you don't specify a 'post_type', comments will be returned even from posts where exclude_from_search = true. (The same is true for 'post_status'.)

I assume that you based your patch on the 'all' logic in WP_Query. But WP_Query has non-null defaults for 'post_status' and 'post_type'. In the case of 'post_status', non-singular queries fall back on 'publish' when 'post_status' is not provided. And 'post_type' will fall back on 'post' in most cases https://core.trac.wordpress.org/browser/tags/4.4.1/src/wp-includes/query.php?marks=3080-3083#L3063

So the semantics are somewhat different. In the case of WP_Query, it's not possible to query for exclude_from_search post types or statuses, except by explicitly whitelisting them. (eg 'post_status' => 'draft') So it makes sense that 'any' would have the same restriction. But the same thing cannot be said for comment queries: excluding 'post_status' will result in 'post_status' being ignored altogether. It feels strange that 'any' would be more restrictive than simply omitting the param.

Setting aside the consistency of the semantics, I'm not sure that comments ought to obey the exclude_from_search status of their parent posts. If a post has a status that excludes it from queries, then of course, you don't want it to appear in WP_Query results. But the status of a post doesn't really have anything to do with its comments.

For these reasons, I'm leaning toward discarding the exclude_from_search logic in the patch. However, this is an unclear issue, so I'd be glad to hear what others have to say.

#3 @kouratoras
4 years ago

Thank you for the feedback @boonebgorges.

I can see your point here. We should provide the same results in case that post_type (or post_status) parameter is any or omitted at all.

I also think that comments have nothing to do with the post type or status and agree to discard the exclude_from_search logic.

I attach the modified patch.

@kouratoras
4 years ago

Modified patch

@boonebgorges
4 years ago

#4 @boonebgorges
4 years ago

Thanks, @kouratoras.

If we aren't going to omit any post types or post statuses, what's the point in validating against them? By that token, if someone passes 'post_type=any', it suggests to me that the 'post_type' clause should be omitted together. See 35512.diff.

@kouratoras
4 years ago

#5 @kouratoras
4 years ago

You are right @boonebgorges, your patch seems complete to me.
There was a typo in the last test function, I attach a patch with the correct name.

Thank you!

#6 @boonebgorges
4 years ago

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

In 36486:

Allow comments to be queried by 'any' post_type or post_status.

Props kouratoras.
Fixes #35512.

Note: See TracTickets for help on using tickets.