Make WordPress Core

Opened 12 years ago

Closed 10 years ago

#20308 closed enhancement (fixed)

Filtering of password protected posts

Reported by: paddelboot's profile paddelboot Owned by: nacin's profile nacin
Milestone: 3.9 Priority: normal
Severity: normal Version: 3.4
Component: Query Keywords: has-patch commit
Focuses: Cc:


to query for password protected posts only or to remove all password protected posts from a query.

Current solution:
Modify the query SQL by using the 'posts_where' filter as suggested here:

Suggested solution:
Make it possible to modify a parameter via 'pre_get_posts' or 'request' - filter, i.e. $query->set( 'post_password', TRUE ).

Attachments (3)

privatequeries.20308.diff (941 bytes) - added by robmiller 11 years ago.
Add has_password var to WP_Query
20308.diff (732 bytes) - added by wonderboymusic 11 years ago.
20308.2.diff (3.3 KB) - added by wonderboymusic 10 years ago.

Download all attachments as: .zip

Change History (19)

#1 @Bueltge
12 years ago

  • Cc frank@… added
  • Keywords changed from password, pre_get_posts, query, request, posts_where to password pre_get_posts, query, request, posts_where
  • Version changed from 3.3.1 to 3.4

#2 @ocean90
12 years ago

  • Component changed from General to Query
  • Keywords needs-patch added; password pre_get_posts query request posts_where removed
  • Type changed from feature request to enhancement

#3 @hughwillfayle
12 years ago

  • Cc t.herzog@… added

#4 @nacin
12 years ago

I could go for post_password as a query variable with these possible values:

  • null (ignore)
  • false (only posts without passwords)
  • true (only posts with passwords)
  • a string (posts with this exact password)

In practice, this kind of type-checking is not particularly feasible in WP_Query, unfortunately. Would have to be two different query vars, such as password_exists and post_password. So for now, something like password_exists or has_password seems like a good approach.

#5 @hearvox
11 years ago

  • Cc mbox@… added

#6 @robmiller
11 years ago

  • Cc r@… added
  • Keywords has-patch added; needs-patch removed

I've added a patch that allows querying for password-protected posts in WP_Query.

It adds a query var called "has_password"; if it's truthy, password-protected posts will be returned; if it's falsey, public posts will be returned.

11 years ago

Add has_password var to WP_Query

#7 @DrewAPicture
11 years ago

  • Cc xoodrew@… added

#8 @robmiller
11 years ago

  • Keywords dev-feedback added

#9 @wonderboymusic
11 years ago

  • Milestone changed from Awaiting Review to 3.7

Modified the patch to do all of the things @nacin said

#10 @nacin
10 years ago

WP_Query + query variables are not really designed for type-checking, so I'm not sure about 20308.diff. Let's consider has_password => true|false|null. And then we could also do post_password = string to actually check the field's value. Thoughts?

#11 @helen
10 years ago

  • Milestone changed from 3.7 to Future Release

Seems like this could use an alternate approach, so punting.

#12 @wonderboymusic
10 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Future Release to 3.9

20308.2.diff supports has_password => true|false|null and allows post_password to be passed explicitly, adding perm => 'readable' if perm is not already set.

Adds Unit Tests.

#13 @wonderboymusic
10 years ago

  • Keywords commit added

#14 @F J Kaiser
10 years ago

This one will knock out the arguments delivered in #16483

#15 @nacin
10 years ago

The latest patch looks good. I actually meant that has_password = null would be the default. So, isset() rather than array_key_exists().

I'm also making it so post_password overrules has_password if specified. No need for them to compete.

#16 @nacin
10 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 27395:

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.

Note: See TracTickets for help on using tickets.