首先,我必须同意@fencepost的回答。然而,我忍不住发布了一个解决方案,所以我们来了!
function __intercept_private_page( $posts, &$wp_query )
{
// remove filter now, so that on subsequent post querying we don\'t get involved!
remove_filter( \'the_posts\', \'__intercept_private_page\', 5, 2 );
if ( !( $wp_query->is_page && empty($posts) ) )
return $posts; // bail, not page with no results
// if you want to explicitly check it *is* private, use the code block below:
/*
if ( !empty( $wp_query->query[\'page_id\'] ) )
$page = get_page( $wp_query->query[\'page_id\'] );
else
$page = get_page_by_path( $wp_query->query[\'pagename\'] );
if ( $page && $page->post_status == \'private\' ) {
// redirect
}
*/
// otherwise assume that if the request was for a page, and no page was found, it was private
wp_redirect( home_url(), 301 );
exit;
}
is_admin() || add_filter( \'the_posts\', \'__intercept_private_page\', 5, 2 );
Update: 要使用的修订代码
the_posts
筛选而不是
posts_results
(在WordPress检查权限之前激发,等等
$posts
尚未“清空”)。