mirror of
git://develop.git.wordpress.org/
synced 2025-02-24 08:33:35 +01:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@27395 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c2dd5d4a75
commit
ed43f002b8
@ -2702,6 +2702,15 @@ class WP_Query {
|
||||
$post_type_cap = $post_type;
|
||||
}
|
||||
|
||||
if ( isset( $q['post_password'] ) ) {
|
||||
$where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
|
||||
if ( empty( $q['perm'] ) ) {
|
||||
$q['perm'] = 'readable';
|
||||
}
|
||||
} elseif ( isset( $q['has_password'] ) ) {
|
||||
$where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
|
||||
}
|
||||
|
||||
if ( 'any' == $post_type ) {
|
||||
$in_search_post_types = get_post_types( array('exclude_from_search' => false) );
|
||||
if ( empty( $in_search_post_types ) )
|
||||
|
@ -543,4 +543,44 @@ class Tests_Query_Results extends WP_UnitTestCase {
|
||||
);
|
||||
$this->assertNotContains( "({$wpdb->posts}.post_status = 'publish') AND", $this->q->request );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 20308
|
||||
*/
|
||||
function test_post_password() {
|
||||
$one = (string) $this->factory->post->create( array( 'post_password' => '' ) );
|
||||
$two = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
|
||||
$three = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
|
||||
|
||||
$args = array( 'post__in' => array( $one, $two, $three ), 'fields' => 'ids' );
|
||||
|
||||
$result1 = $this->q->query( array_merge( $args, array( 'has_password' => true ) ) );
|
||||
$this->assertEqualSets( array( $two, $three ), $result1 );
|
||||
$result2 = $this->q->query( array_merge( $args, array( 'has_password' => false ) ) );
|
||||
$this->assertEquals( array( $one ), $result2 );
|
||||
|
||||
// This is equivalent to not passing it at all.
|
||||
$result3 = $this->q->query( array_merge( $args, array( 'has_password' => null ) ) );
|
||||
$this->assertEqualSets( array( $one, $two, $three ), $result3 );
|
||||
|
||||
// If both arguments are passed, only post_password is considered.
|
||||
$result4 = $this->q->query( array_merge( $args, array( 'has_password' => true, 'post_password' => '' ) ) );
|
||||
$this->assertEquals( array( $one ), $result4 );
|
||||
$result5 = $this->q->query( array_merge( $args, array( 'has_password' => false, 'post_password' => '' ) ) );
|
||||
$this->assertEquals( array( $one ), $result5 );
|
||||
$result6 = $this->q->query( array_merge( $args, array( 'has_password' => null, 'post_password' => '' ) ) );
|
||||
$this->assertEquals( array( $one ), $result6 );
|
||||
|
||||
$result7 = $this->q->query( array_merge( $args, array( 'has_password' => true, 'post_password' => 'burrito' ) ) );
|
||||
$this->assertEqualSets( array( $two, $three ), $result7 );
|
||||
$result8 = $this->q->query( array_merge( $args, array( 'has_password' => false, 'post_password' => 'burrito' ) ) );
|
||||
$this->assertEqualSets( array( $two, $three ), $result8 );
|
||||
$result9 = $this->q->query( array_merge( $args, array( 'has_password' => null, 'post_password' => 'burrito' ) ) );
|
||||
$this->assertEqualSets( array( $two, $three ), $result9 );
|
||||
|
||||
$result10 = $this->q->query( array_merge( $args, array( 'post_password' => '' ) ) );
|
||||
$this->assertEquals( array( $one ), $result10 );
|
||||
$result11 = $this->q->query( array_merge( $args, array( 'post_password' => 'burrito' ) ) );
|
||||
$this->assertEqualSets( array( $two, $three ), $result11 );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user