Introduce `$comment_status` and `$ping_status` params for `WP_Query`.

Props birgire.
Fixes #35601.

git-svn-id: https://develop.svn.wordpress.org/trunk@36403 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-01-26 02:55:15 +00:00
parent af5a3b663f
commit 14b6dbebd2
2 changed files with 41 additions and 0 deletions

View File

@ -1484,6 +1484,7 @@ class WP_Query {
* @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded
* search terms, by prepending a hyphen.
* @since 4.5.0 Removed the `$comments_popup` parameter.
* Introduced the `$comment_status` and `$ping_status` parameters.
* @access public
*
* @param string|array $query {
@ -1500,6 +1501,7 @@ class WP_Query {
* @type array $category__in An array of category IDs (OR in, no children).
* @type array $category__not_in An array of category IDs (NOT in).
* @type string $category_name Use category slug (not name, this or any children).
* @type string $comment_status Comment status.
* @type int $comments_per_page The number of comments to return per page.
* Default 'comments_per_page' option.
* @type array $date_query An associative array of WP_Date_Query arguments.
@ -1545,6 +1547,7 @@ class WP_Query {
* @type int $page_id Page ID.
* @type string $pagename Page slug.
* @type string $perm Show posts if user has the appropriate capability.
* @type string $ping_status Ping status.
* @type array $post__in An array of post IDs to retrieve, sticky posts will be included
* @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
* @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
@ -3036,6 +3039,14 @@ class WP_Query {
$where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
}
if ( ! empty( $q['comment_status'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->posts.comment_status = %s ", $q['comment_status'] );
}
if ( ! empty( $q['ping_status'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->posts.ping_status = %s ", $q['ping_status'] );
}
if ( 'any' == $post_type ) {
$in_search_post_types = get_post_types( array('exclude_from_search' => false) );
if ( empty( $in_search_post_types ) )

View File

@ -492,4 +492,34 @@ class Tests_Query extends WP_UnitTestCase {
$this->assertContains( 'LIMIT 5, 5', $q->request );
}
/**
* @ticket 35601
*/
public function test_comment_status() {
$p1 = self::factory()->post->create( array( 'comment_status' => 'open' ) );
$p2 = self::factory()->post->create( array( 'comment_status' => 'closed' ) );
$q = new WP_Query( array(
'fields' => 'ids',
'comment_status' => 'closed',
) );
$this->assertSame( array( $p2 ), $q->posts );
}
/**
* @ticket 35601
*/
public function test_ping_status() {
$p1 = self::factory()->post->create( array( 'ping_status' => 'open' ) );
$p2 = self::factory()->post->create( array( 'ping_status' => 'closed' ) );
$q = new WP_Query( array(
'fields' => 'ids',
'ping_status' => 'closed',
) );
$this->assertSame( array( $p2 ), $q->posts );
}
}