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
    rewrite and publicly_queryable are in conflict with each other. You can't have rewrite rules for singular or archive views on a post type that's not publicly queryable. – Milo Sep 10 at 1:34
  • Thanks @Milo. If I understand you correctly, setting rewrite to false should fix it? – emersonthis Sep 11 at 12:34
  • @Milo That did not solve my problem. It changed the URL to a query var (ex: /?tattooer=some-body) but it still recognized the route and did not generate a 404. – emersonthis Sep 15 at 16:41
  • 1
    Add any /?nonsense_var=nonsense to your root URL and note that those also don't generate 404s. Why is that a problem? – Milo Sep 15 at 17:11
  • I want erroneous links to generate proper 404s instead of getting redirected to my home page. – emersonthis Sep 16 at 18:49

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.

  • +1 seems to be the easiest way without having to create your own archive with the headache of handling pagination. OTOH can't you just register the post type with no front end access and add a rewrite rule for the archive? – Mark Kaplun Sep 16 at 5:07
  • @MarkKaplun That way you can obtain a archive + no singular, but if you are able to guess the singular URL (not that hard) whenever you try to access it you got a redirect to home page, not 404. If you really want to obtain a 404 (lot of people don't like redirects from non-existent URLs) you have to stick with this. Also, register the rewrite rule is an action + a callback, not much different than code above, yes you can save a couple of lines of code, but you would need to flush rewrite rules... so I don't see that as a simpler solution. – gmazzap Sep 16 at 14:32
  • yeh, i assumed it is not simpler – Mark Kaplun Sep 16 at 14:56
  • Thanks. I don't need the archives either. The content only appears on one page with a custom (WP_Query) loop. So it literally has no routes (accept the admin interfaces). – emersonthis Sep 16 at 18:52
  • @emersonthis than you should also set has_archive to false. And by also setting both publicly_queryable and rewrite to false as well, that should be fine. At that point the code I proposed should be removed and that's it. Yes, some people can go to the URL ` /?tattooer=xxx` but that in your site is non-sensical query var appended to home page URL, and it is ok it does not 404, that's default behavior of WP (and does not affect SEO, if you are wondering). – gmazzap Sep 17 at 16:40

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.