Make WordPress Core

Changeset 27395


Ignore:
Timestamp:
03/04/2014 07:44:28 AM (11 years ago)
Author:
nacin
Message:

Add has_password and post_password query variables to WP_Query.

  • has_password true means posts with passwords, false means posts without.
  • post_password can query for posts with a particular password.

props wonderboymusic, robmiller.
fixes #20308.

Location:
trunk
Files:
2 edited

Legend:

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

    r27304 r27395  
    27032703        }
    27042704
     2705        if ( isset( $q['post_password'] ) ) {
     2706            $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
     2707            if ( empty( $q['perm'] ) ) {
     2708                $q['perm'] = 'readable';
     2709            }
     2710        } elseif ( isset( $q['has_password'] ) ) {
     2711            $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
     2712        }
     2713
    27052714        if ( 'any' == $post_type ) {
    27062715            $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  • trunk/tests/phpunit/tests/query/results.php

    r27067 r27395  
    544544        $this->assertNotContains( "({$wpdb->posts}.post_status = 'publish') AND", $this->q->request );
    545545    }
     546
     547    /**
     548     * @ticket 20308
     549     */
     550    function test_post_password() {
     551        $one   = (string) $this->factory->post->create( array( 'post_password' => '' ) );
     552        $two   = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
     553        $three = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
     554
     555        $args = array( 'post__in' => array( $one, $two, $three ), 'fields' => 'ids' );
     556
     557        $result1 = $this->q->query( array_merge( $args, array( 'has_password' => true ) ) );
     558        $this->assertEqualSets( array( $two, $three ), $result1 );
     559        $result2 = $this->q->query( array_merge( $args, array( 'has_password' => false ) ) );
     560        $this->assertEquals( array( $one ), $result2 );
     561
     562        // This is equivalent to not passing it at all.
     563        $result3 = $this->q->query( array_merge( $args, array( 'has_password' => null ) ) );
     564        $this->assertEqualSets( array( $one, $two, $three ), $result3 );
     565
     566        // If both arguments are passed, only post_password is considered.
     567        $result4 = $this->q->query( array_merge( $args, array( 'has_password' => true, 'post_password' => '' ) ) );
     568        $this->assertEquals( array( $one ), $result4 );
     569        $result5 = $this->q->query( array_merge( $args, array( 'has_password' => false, 'post_password' => '' ) ) );
     570        $this->assertEquals( array( $one ), $result5 );
     571        $result6 = $this->q->query( array_merge( $args, array( 'has_password' => null, 'post_password' => '' ) ) );
     572        $this->assertEquals( array( $one ), $result6 );
     573
     574        $result7 = $this->q->query( array_merge( $args, array( 'has_password' => true, 'post_password' => 'burrito' ) ) );
     575        $this->assertEqualSets( array( $two, $three ), $result7 );
     576        $result8 = $this->q->query( array_merge( $args, array( 'has_password' => false, 'post_password' => 'burrito' ) ) );
     577        $this->assertEqualSets( array( $two, $three ), $result8 );
     578        $result9 = $this->q->query( array_merge( $args, array( 'has_password' => null, 'post_password' => 'burrito' ) ) );
     579        $this->assertEqualSets( array( $two, $three ), $result9 );
     580
     581        $result10 = $this->q->query( array_merge( $args, array( 'post_password' => '' ) ) );
     582        $this->assertEquals( array( $one ), $result10 );
     583        $result11 = $this->q->query( array_merge( $args, array( 'post_password' => 'burrito' ) ) );
     584        $this->assertEqualSets( array( $two, $three ), $result11 );
     585    }
    546586}
Note: See TracChangeset for help on using the changeset viewer.