Introduce `post_name__in` parameter for `WP_Query`.

Props enshrined.
Fixes #33065.

git-svn-id: https://develop.svn.wordpress.org/trunk@33653 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-08-20 02:18:05 +00:00
parent 9bb010f2a1
commit 5e9a216cf7
2 changed files with 37 additions and 2 deletions

View File

@ -1434,7 +1434,7 @@ class WP_Query {
$array[$key] = '';
}
$array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
$array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 'post_name__in',
'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
'author__in', 'author__not_in' );
@ -1451,6 +1451,7 @@ class WP_Query {
* @since 1.5.0
* @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
* array key to `$orderby`.
* @since 4.3.0 Introduced the `$post_name__in` parameter.
* @access public
*
* @param string|array $query {
@ -1527,6 +1528,7 @@ class WP_Query {
* @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
* @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
* 'posts_per_page' when is_archive(), or is_search() are true.
* @type array $post_name__in An array of post slugs that results must match.
* @type string $s Search keyword.
* @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
* @type array $search_terms Array of search terms.
@ -2603,6 +2605,7 @@ class WP_Query {
unset($ptype_obj);
}
// Parameters related to 'post_name'.
if ( '' != $q['name'] ) {
$q['name'] = sanitize_title_for_query( $q['name'] );
$where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
@ -2647,9 +2650,11 @@ class WP_Query {
$q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
$q['name'] = $q['attachment'];
$where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
} elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
$q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
$where .= " AND $wpdb->posts.post_name IN ('" . implode( "' ,'", $q['post_name__in'] ) . "')";
}
if ( intval($q['comments_popup']) )
$q['p'] = absint($q['comments_popup']);

View File

@ -302,4 +302,34 @@ class Tests_Post_Query extends WP_UnitTestCase {
$this->assertNotContains( 'DESC', $q5->request );
$this->assertNotContains( 'ASC', $q5->request );
}
/**
* Tests the post_name__in attribute of WP_Query.
*
* @ticket 33065
*/
public function test_post_name__in() {
$q = new WP_Query();
$post_ids[0] = $this->factory->post->create( array( 'post_title' => 'woo', 'post_date' => '2015-07-23 00:00:00' ) );
$post_ids[1] = $this->factory->post->create( array( 'post_title' => 'hoo', 'post_date' => '2015-07-23 00:00:00' ) );
$post_ids[2] = $this->factory->post->create( array( 'post_title' => 'test', 'post_date' => '2015-07-23 00:00:00' ) );
$post_ids[3] = $this->factory->post->create( array( 'post_title' => 'me', 'post_date' => '2015-07-23 00:00:00' ) );
$requested = array( $post_ids[0], $post_ids[3] );
$q->query( array(
'post_name__in' => array( 'woo', 'me' ),
'fields' => 'ids',
) );
$actual_posts = $q->get_posts();
$this->assertEqualSets( $requested, $actual_posts );
$requested = array( $post_ids[1], $post_ids[2] );
$q->query( array(
'post_name__in' => array( 'hoo', 'test' ),
'fields' => 'ids',
) );
$actual_posts = $q->get_posts();
$this->assertEqualSets( $requested, $actual_posts );
}
}