'How to list Wordpress Password Protected Pages?
Is there anyway in wordpress to return a sorted list of all the password protected pages in a wordpress site?
If so how do I go about this?
I basically want one page showing a list of all the pages that are password protected...
Thanks! :)
Solution 1:[1]
I think this is the simplest answer to the question:
$page_ids = get_posts( [
'post_type' => 'page',
'has_password' => true,
'posts_per_page' => - 1,
'post_status' => 'any',
'fields' => 'ids',
] );
wp_list_pages( 'include=' . implode( ',', $page_ids ) );
If you want to create your own list:
$page_ids = get_posts( [
'post_type' => 'page',
'has_password' => true,
'posts_per_page' => - 1,
'post_status' => 'any',
'fields' => 'ids',
] );
foreach ( $page_ids as $page_id ) {
// get_permalink( $page_id );
}
This is just a starting point.
You can play with get_posts or WP_Query to achieve better results.
Solution 2:[2]
// Filter to hide protected posts
function exclude_protected($where) {
global $wpdb;
return $where .= " AND {$wpdb->posts}.post_password = '' ";
}
// Decide where to display them
function exclude_protected_action($query) {
if( !is_single() && !is_page() && !is_admin() ) {
add_filter( 'posts_where', 'exclude_protected' );
}
}
// Action to queue the filter at the right time
add_action('pre_get_posts', 'exclude_protected_action');
I'd suggest using a filter, the piece of code above will exclude all password protected posts, with some editing you'll be able to get all password protected posts.
More info: https://codex.wordpress.org/Using_Password_Protection#Hiding_Password_Protected_Posts
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
