Search code examples
phpwordpresscustom-post-type

Wordpress how to disable child pages for custom post types


I have a custom post type called designs.

designs is registered like this:

register_post_type(
  'Designs',
  theme_build_post_args(
    // $slug, $singular, $plural
    'designs',
    'Design',
    'Designs',
    array(
      'menu_icon' => 'dashicons-admin-appearance',
      'menu_position' => 20,
      'has_archive' => false,
      'public' => false,
      'supports' => array('title'),
    )
  )
);

For this post type, I don't want the following URLs to be accessible:

  • /designs
  • /designs/post_name

I'm just using this post type to extract data for a block, it's not a standard level one and level two layout.

From what I've seen online, 'has_archive'=> false is what I need to achieve the above, however, it doesn't work for me? In the backend, the option to "view" a design post exists, which shouldn't be there as it shouldn't be generating a page for it?


Solution

  • Try to add this argument: 'publicly_queryable' => false when you register your custom post type. It should do the trick for you!

    From register_post_typeDocs:

    publicly_queryable:
    Whether queries can be performed on the front end as part of parse_request().
    If false, previewing/viewing of your custom post will return 404s.

    register_post_type(
      'Designs',
      theme_build_post_args(
        // $slug, $singular, $plural
        'designs', 'Design', 'Designs',
        array(
          'menu_icon' => 'dashicons-admin-appearance',
          'menu_position'=> 20,
          'has_archive'=> false,
          'public' => false,
          'publicly_queryable' => false,
          'supports'=> array('title'),
        )
      )
    );
    

    Let me know if you could get it to work!