How to create a custom post type without letting WordPress assign a URL

OK, so there are some arguments of register_post_type that you should use.

The crucial arguments for you are:

  • public – Controls how the type is visible to authors (show_in_nav_menus, show_ui) and readers (exclude_from_search, publicly_queryable). If it’s false, then exclude_from_search will be true, publicly_queryable
    false, show_in_nav_menus – false, and show_ui – false. So the CPT will be hidden all the way.

  • exclude_from_search – Whether to exclude posts with this post type from front end search results. Default: value of the opposite of public argument.

  • publicly_queryable – Whether queries can be performed on the front end as part of parse_request(). Default: value of public argument. So we have to et it true.

  • show_ui – Whether to generate a default UI for managing this post type in the admin. Default: value of public argument.

  • rewrite – Triggers the handling of rewrites for this post type. To prevent rewrites, set to false. Default: true and use $post_type as slug. So we have to set it false.

Below you can find the code:

$labels = array( /*removed for example*/ );

$args = array(
    'labels'             => $labels,
    'description'        => __( 'Description.', 'your-plugin-textdomain' ),
    'public'             => false,
    'show_ui'            => true,
    'rewrite'            => false,
    'capability_type'    => 'post',
    'hierarchical'       => false,
    /* ... Any other arguments like menu_icon, and so on */
    'supports'           => array( /* list of supported fields */ )
);

register_post_type( 'customer', $args );

This generator maybe helpful, if you don’t want to learn all the arguments:
https://generatewp.com/post-type/

And the list of all arguments, as always, you can find in Codex:
https://codex.wordpress.org/Function_Reference/register_post_type

Leave a Comment