I have a custom post_type that I registered with the following settings:

  'labels' => piklist('post_type_labels', 'Tattooer')
  ,'title' => __('Enter the name of the tattooer')
  ,'supports' => array(
    'title',
    'editor',
    'thumbnail',
    'page-attributes'
  )
  ,'public' => true
  ,'has_archive' => true
  ,'rewrite' => array(
    'slug' => 'tattooer'
  )
  ,'publicly_queryable' => false
  ,'capability_type' => 'post'
  ,'edit_columns' => array(
    'title' => __('Name')
  )
  ,'hide_meta_box' => array(
    'author'
  )

I don’t want any URLs to single tattooer posts. My understanding is that 'publicly_queryable' => false is the tool for this job. But when I set it to false, the single URL is still active (Ex: /tattooer/foobar/) but redirects to the homepage. I think the intended behavior is that it should be a 404, which is what I want. What am I missing?

Notice that I’m using the wonderful Piklist plugin to expedite the registration of custom post_type(s), but I’ve tested without it as well and the results were the same.

My information is based on the documentation and this very similar question.

1 Answer
1

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 Reply

Your email address will not be published. Required fields are marked *