Make WordPress Core

Opened 3 years ago

Closed 18 months ago

#20308 closed enhancement (fixed)

Filtering of password protected posts

Reported by: paddelboot Owned by: 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: http://stackoverflow.com/questions/7538959/how-to-exclude-password-protected-posts-in-wordpress-loop

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 3 years ago.
Add has_password var to WP_Query
20308.diff (732 bytes) - added by wonderboymusic 2 years ago.
20308.2.diff (3.3 KB) - added by wonderboymusic 20 months ago.

Download all attachments as: .zip

Change History (19)

comment:1 @Bueltge3 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

comment:2 @ocean903 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

comment:3 @hughwillfayle3 years ago

  • Cc t.herzog@… added

comment:4 @nacin3 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.

comment:5 @hearvox3 years ago

  • Cc mbox@… added

comment:6 @robmiller3 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.

@robmiller3 years ago

Add has_password var to WP_Query

comment:7 @DrewAPicture3 years ago

  • Cc xoodrew@… added

comment:8 @robmiller3 years ago

  • Keywords dev-feedback added

@wonderboymusic2 years ago

comment:9 @wonderboymusic2 years ago

  • Milestone changed from Awaiting Review to 3.7

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

comment:10 @nacin2 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?

comment:11 @helen2 years ago

  • Milestone changed from 3.7 to Future Release

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

@wonderboymusic20 months ago

comment:12 @wonderboymusic20 months 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.

comment:13 @wonderboymusic19 months ago

  • Keywords commit added

comment:14 @F J Kaiser18 months ago

This one will knock out the arguments delivered in #16483

comment:15 @nacin18 months 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.

comment:16 @nacin18 months 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.