Opened 10 years ago
Closed 10 years ago
#29612 closed enhancement (fixed)
Query for multiple comment statuses
Reported by: | ebinnion | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.1 | Priority: | normal |
Severity: | normal | Version: | 4.0 |
Component: | Comments | Keywords: | needs-patch |
Focuses: | Cc: |
Description
I am currently working on a project where I need to query both trashed and approved comments. Currently, I am doing this by calling get_comments twice and then merging the results.
I think it would be more ideal if a developer could query for multiple comment statuses by passing in an array of statuses.
For example:
$comments = get_comments( array( 'status' => array( 'trash', 'approve' ) ) );
My use case is that I want to show trashed comments (with a "This comment was trashed notice' ) as well as approved comments in an effort to better preserve conversation threads. Here is an example:
Attachments (5)
Change History (20)
#3
follow-up:
↓ 4
@
10 years ago
- Keywords needs-unit-tests added
There is a lot of history in those few lines of code. See #21101. I don't think this is as simple as wrapping a foreach around it, as 'all' probably needs to be handled a bit differently, etc.
Also, would need unit tests.
#4
in reply to:
↑ 3
@
10 years ago
Replying to nacin:
There is a lot of history in those few lines of code. See #21101. I don't think this is as simple as wrapping a foreach around it, as 'all' probably needs to be handled a bit differently, etc.
Also, would need unit tests.
That was an interesting read, thanks for that. I believe my patch retains the existing handling for a status of 'all': it's still "( comment_approved = '0' OR comment_approved = '1' )"
Although, it occurs to me that if 'all' is included as one of the statuses in an array, it will be ignored. I'm not sure what the behaviour should be in that situation.
#5
@
10 years ago
I have added an alternative patch that will allow an array of comment statuses.
karpstrucking brought up a good point about how to handle all
in the array of statuses. I took the approach to add the approve and hold statuses if not already present.
#6
@
10 years ago
- Keywords needs-testing needs-unit-tests removed
I have updated the patch to include a couple of unit tests. I have verified that the unit tests are passing with my suggested patch.
vagrant@vvv:/srv/www/wordpress-develop$ phpunit --group comment Installing... Running as single site... To run multisite, use -c tests/phpunit/multisite.xml Not running ajax tests... To execute these, use --group ajax. PHPUnit 4.0.20 by Sebastian Bergmann. Configuration read from /srv/www/wordpress-develop/phpunit.xml.dist ........................ Time: 3.89 seconds, Memory: 60.50Mb OK (24 tests, 178 assertions) vagrant@vvv:/srv/www/wordpress-dev
#7
@
10 years ago
I'm also working on a project where multiple statuses need to be returned at the same time. The patch by ebinnion would be ideal. The work around at the moment is to combine the statuses together into a single 'status' then filter on 'comments_clauses' to explode it back out.
I'd also like to see it combined with ryans patch ( https://core.trac.wordpress.org/ticket/21101#comment:27 ) which included a 'full' option which then allowed no comment_approved column to be part of the SQL. The reason for that is if you are requesting a custom comment type then you may well want all regardless of status.
#9
@
10 years ago
- Milestone changed from Awaiting Review to 4.1
- Owner set to boonebgorges
- Status changed from new to accepted
In [29980], I added some more systematic unit tests for the existing functionality.
29612.patch adds support for multiple comment statuses:
- Accepts an array or a comma-separated string
- A value of 'any' will cause the 'comment_approved' clause to be skipped, returning comments regardless of 'comment_approved' value. 'any' will override any status it's included with (eg
array( 'any', 'approve' )
- The 'any' functionality means that sometimes the 'approved' WHERE clause will be empty - and, in certain cases, the WP_Comment_Query SQL query will not contain a WHERE clause at all. This means that the current way of concatenating the WHERE clause won't work. I've changed it so that it builds an array of subclauses, which get assembled just before the final query string is built. Note that in the case of date_query, meta_query, and get_search_sql(), this meant stripping the leading ' AND ' from the SQL chunks. A bit ugly, but necessary for backward compatibility.
This ticket was mentioned in IRC in #wordpress-dev by boonebgorges. View the logs.
10 years ago
This ticket was mentioned in IRC in #wordpress-dev by doc-bot. View the logs.
10 years ago
#13
@
10 years ago
- Keywords needs-patch added; has-patch removed
- Resolution fixed deleted
- Status changed from closed to reopened
[30084] brokes /wp-admin/edit-comments.php. The All view includes spam comments now too.
Related: #20006, #21571.