Make WordPress Core

Changeset 33653


Ignore:
Timestamp:
08/20/2015 02:18:05 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce post_name__in parameter for WP_Query.

Props enshrined.
Fixes #33065.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r33095 r33653  
    14351435        }
    14361436
    1437         $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
     1437        $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 'post_name__in',
    14381438            'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
    14391439            'author__in', 'author__not_in' );
     
    14521452     * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
    14531453     *              array key to `$orderby`.
     1454     * @since 4.3.0 Introduced the `$post_name__in` parameter.
    14541455     * @access public
    14551456     *
     
    15281529     *     @type int          $posts_per_archive_page  The number of posts to query for by archive page. Overrides
    15291530     *                                                 'posts_per_page' when is_archive(), or is_search() are true.
     1531     *     @type array        $post_name__in           An array of post slugs that results must match.
    15301532     *     @type string       $s                       Search keyword.
    15311533     *     @type int          $second                  Second of the minute. Default empty. Accepts numbers 0-60.
     
    26042606        }
    26052607
     2608        // Parameters related to 'post_name'.
    26062609        if ( '' != $q['name'] ) {
    26072610            $q['name'] = sanitize_title_for_query( $q['name'] );
     
    26482651            $q['name'] = $q['attachment'];
    26492652            $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
    2650         }
    2651 
     2653        } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
     2654            $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
     2655            $where .= " AND $wpdb->posts.post_name IN ('" . implode( "' ,'", $q['post_name__in'] ) . "')";
     2656        }
    26522657
    26532658        if ( intval($q['comments_popup']) )
  • trunk/tests/phpunit/tests/post/query.php

    r31286 r33653  
    303303        $this->assertNotContains( 'ASC', $q5->request );
    304304    }
     305
     306    /**
     307     * Tests the post_name__in attribute of WP_Query.
     308     *
     309     * @ticket 33065
     310     */
     311    public function test_post_name__in() {
     312        $q = new WP_Query();
     313
     314        $post_ids[0] = $this->factory->post->create( array( 'post_title' => 'woo', 'post_date' => '2015-07-23 00:00:00' ) );
     315        $post_ids[1] = $this->factory->post->create( array( 'post_title' => 'hoo', 'post_date' => '2015-07-23 00:00:00' ) );
     316        $post_ids[2] = $this->factory->post->create( array( 'post_title' => 'test', 'post_date' => '2015-07-23 00:00:00' ) );
     317        $post_ids[3] = $this->factory->post->create( array( 'post_title' => 'me', 'post_date' => '2015-07-23 00:00:00' ) );
     318
     319        $requested = array( $post_ids[0], $post_ids[3] );
     320        $q->query( array(
     321            'post_name__in' => array( 'woo', 'me' ),
     322            'fields' => 'ids',
     323        ) );
     324        $actual_posts = $q->get_posts();
     325        $this->assertEqualSets( $requested, $actual_posts );
     326
     327        $requested = array( $post_ids[1], $post_ids[2] );
     328        $q->query( array(
     329            'post_name__in' => array( 'hoo', 'test' ),
     330            'fields' => 'ids',
     331        ) );
     332        $actual_posts = $q->get_posts();
     333        $this->assertEqualSets( $requested, $actual_posts );
     334    }
    305335}
Note: See TracChangeset for help on using the changeset viewer.