Saltar navegación

[Resuelto] Hide cpt archive from non logged in users

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 4 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 -

Zona horaria del colaborador: Asia/Kolkata (GMT+05:30)

Este tema contiene 4 respuestas, tiene 2 mensajes.

Última actualización por hugoC-3 4 years, 8 months ago.

Asistido por: Minesh.

Autor
Mensajes
#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? enlace oculto

#2005235

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: 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
Colaborador

Idiomas: Inglés (English )

Zona horaria: 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!