Opened 10 years ago
Closed 7 years ago
#28399 closed enhancement (fixed)
Allow WP_Query to query by comment_count column of CPT/posts table
Reported by: | ramon fincken | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.9 | Priority: | normal |
Severity: | normal | Version: | 4.0 |
Component: | Query | Keywords: | has-patch has-unit-tests commit |
Focuses: | Cc: |
Description (last modified by )
In the current WP_Query, it is possible to order results by comment_count ASC/DESC.
But is it not possible to query the table based on this CPT/posts table column. Therefor it is impossible to (for example) query posts without any comment or posts having over 10 comments.
I propose a diff to use WP_Query in the following way (building on the syntax from meta_query and tax_query)
a) Easy call:
$args['comment_count'] = n; n >= 0
b) Extended call:
$args['comments']['count'] = n; n >= 0
Optional in this call:
$args['comments']['compare'] = Compare; Compare: =, !=, >, >=, <, <=
I will extend this ticket with a patch diff file (trunk) along with a testset (has-patch).
Attachments (5)
Change History (20)
#4
in reply to:
↑ 2
@
10 years ago
Replying to SergeyBiryukov:
Thanks for adjusting, you beat me to it ;)
#5
@
10 years ago
- Keywords needs-patch added; has-patch needs-testing removed
- Milestone changed from Awaiting Review to Future Release
I this this is a pretty good idea. A couple of comments (no pun intended):
- Let's not add two new params ('comment' and 'comment_count'). This seems unnecessary and confusing to me. I suggest having one: 'comment_count'. You can pass an array of 'count' and 'compare', or you can pass it a number, which gets parsed into
array( 'count' => $number, 'compare' => '=' )
. - The unit tests look really comprehensive - thanks so much for that. Let's move them to
tests/phpunit/tests/query/commentCount.php
, since this is a new feature for WP_Query. - In the unit tests: no need to do
http_build_query()
- just pass an array. No need to set dummy 'post_content' or 'post_type' on the factory posts - just use the defaults ($p1 = $this->factory->post->create()
, with no arguments). Testingfound_posts
is not precise enough - please get the found post IDs and useassertEqualSets
to compare with$this->q->posts
. I recommend using the following arguments forWP_Query
, to minimize overhead:
$this->q->query( array( 'fields' => 'ids', 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'comment_count' => array( 'count' => 5, 'compare' => '>', ), ) ); $expected = array( $p1, $p2 ); $this->assertEqualSets( $expected, $this->q->posts );
The functionality itself looks pretty nice, so with some of these changes I think we can put this in. Thanks for the contribution!
#7
@
7 years ago
Rewrote this on WordCamp Europe (Paris) to fit with WP 4.8
Uploading the patch and unit tests now
You can use it the integer way:
comment_count = INT
or array
comment_count = ARRAY ( value = INT [, compare = STRING ] )
for instance
<?php 'comment_count' => 1
or
<?php 'comment_count' => array( 'value' => 6, 'compare' => '<' )
This ticket was mentioned in Slack in #core by ramonfincken. View the logs.
7 years ago
#10
@
7 years ago
- Milestone changed from Future Release to 4.9
Thank you for circling back to this ticket, @ramon fincken. The patch, and especially the tests, are very good.
I've made some modifications in 28399.diff:
- Added documentation and changed some formatting to match coding standards.
- In the tests, I've used a few convenience methods to simplify your code (you'd implemented your own versions of WP's
wp_list_pluck()
andassertEqualSets()
) - Rearranged the way the SQL is assembled to be a bit more readable.
Please review the changes and make sure I've captured everything.
#11
@
7 years ago
I will check this tonight or tomorrow. Quick one yet: if we use prepare, why still use the intval on top?
#13
follow-up:
↓ 14
@
7 years ago
Yes, lets GO :)
The rewritten unit tests are much better readable. Thanks for that.
Do I need to do something at this point?
#14
in reply to:
↑ 13
@
7 years ago
- Keywords commit added
Replying to ramon fincken:
Do I need to do something at this point?
Nope :-) @boonebgorges will likely give everything another a once over and then commit if everything looks good.
Patch