Opened 7 years ago
Closed 7 years ago
#35512 closed defect (bug) (fixed)
Inconsistent behavior when passing `post_status=any` to WP_Comment_Query
Reported by: |
|
Owned by: |
|
---|---|---|---|
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)
Change History (10)
#2
@
7 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
@
7 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.
#4
@
7 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.
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.