Php 除非已登录,否则对用户隐藏CPT

Php 除非已登录,否则对用户隐藏CPT,php,wordpress,Php,Wordpress,我有一个名为Documents的自定义帖子类型,它有一个名为documentcats的分类法,所有这些都只需要登录用户访问 我从另一个线程中使用了这个: add_filter( 'wp', 'f040925b_redirect', 0 ); function f040925b_redirect( $content ) { global $post; if( ( $post->post_type == 'documents'

我有一个名为
Documents
的自定义帖子类型,它有一个名为
documentcats
的分类法,所有这些都只需要登录用户访问

我从另一个线程中使用了这个:

add_filter( 'wp', 'f040925b_redirect', 0 );
function f040925b_redirect( $content ) {
    global $post;
    if(
        (
            $post->post_type == 'documents'
            ||
            is_post_type_archive( 'documents' )
            ||
            is_tax( 'documentcat' )
        )
        &&
        !is_user_logged_in()
    ) {
        wp_redirect( get_home_url() );
        exit;
    }
    return $content;
}
然而,由于帖子是可搜索的,这会阻止wordpress搜索功能工作,并将所有搜索重定向回主页

有谁能建议我如何完全隐藏帖子类型及其分类页面、归档页面等。。从任何未登录但未破坏搜索功能的用户那里


谢谢

我检查过了,它似乎在工作。但是,如果您只搜索将显示文档发布类型的术语,则它将移动到主页。您是否创建了自定义发布类型?如果是这样,您应该已经在
init
操作中注册了它,此时用户是否已经通过身份验证。因此,在注册帖子类型之前,您可以使用
is\u user\u logged\u
。如果他们没有登录,它甚至不存在(它仍然存在于您的数据库和登录用户中,但WordPress不会意识到它存在于未登录用户中)。