Skip Navigation

[Resolved] Hide cpt archive from non logged in users

This thread is resolved. Here is a description of the problem and solution.

Problem:
Hide cpt archive from non logged in users

Solution:
Toolset Access doesn't include post type archive restrictions.

To restrict the archive for non-loggedin user, You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/hide-cpt-archive-from-non-logged-in-users/#post-2005235

Relevant Documentation:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by hugoC-3 3 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2005023

Tell us what you are trying to do? I set my cpt to private using Access groups, but the cpt archive page is still visible. How can I hide this page from non logged in users?

Is there any documentation that you are following? https://toolset.com/forums/topic/non-logged-in-users-can-access-a-category-page-though-the-cpt-is-set-to-private/#post-1799989

What is the link to your site? hidden link

#2005235

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset Access doesn't include post type archive restrictions. If only admins are allowed to see the full list, it might be simplest to show no archives on the front-end, and let admins see the list in wp-admin.

That can be achieved by editing your post type from:
=> Toolset > Post Types => Edit your post type and uncheck the Option "has_archive" to turn off the archive for this custom post type.

But to restrict the archive for non-loggedin user, I suggest to use the following snippet:

Can you please try to add the following code to "Custom Code" section or current theme's functions.php file:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_action('pre_get_posts', 'func_disable_post_type_archive');
function func_disable_post_type_archive($qry) {

if (is_admin()) return;

if (is_post_type_archive('book') and !is_user_logged_in()){
        $qry->set_404();
}
}

Where:
- change the 'book' with your original post type slug.

I hope above solution will help you to resolve your issue.

#2008179

Hello Minesh,

Thank you, this works.
Is it also possible to display a content template with a custom message instead of a 404?

Regards
Hugo

#2008199

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - as you are using pre_get_posts, the hook will run before fetching the posts and that is why there is no way to assign content template.

#2008331

My issue is resolved now. Thank you!