‘publicly_queryable’ => false not working as expected for custom post_type

If you want archives, but not singular view, then 'publicly_queryable' can’t help you.

If you want that visiting a singular post send a 404… just do it.

You can hook 'template_redirect' and manually set the 404:

add_action(
    'template_redirect',
    function () {
        if (is_singular('tattooer')) {
           global $wp_query;
           $wp_query->posts = [];
           $wp_query->post = null;
           $wp_query->set_404();
           status_header(404);
           nocache_headers();
        }
    }
);

Unfortunately there’s no a function in WP to force 404, and you need to do it manually.

Leave a Comment