如何列出Wordpress密码保护的页面


How to list Wordpress Password Protected Pages?

wordpress中是否有任何方法可以返回WordPress站点中所有受密码保护的页面的排序列表?

如果是这样,我该怎么做?

我基本上想要一个页面显示所有受密码保护的页面的列表......

谢谢! :)

我认为这是

这个问题最简单的答案:

$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 ) );

如果要创建自己的列表:

$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 );
}

这只是一个起点。您可以使用get_postsWP_Query来获得更好的结果。

// 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');

我建议使用过滤器,上面的代码段将排除所有受密码保护的帖子,通过一些编辑,您将能够获得所有受密码保护的帖子。

更多信息: https://codex.wordpress.org/Using_Password_Protection#Hiding_Password_Protected_Posts