Let me give you a scenario of what I’m trying to do. I have a custom post type “customer” and I can go in and “add new customer”. Here I see my custom fields such as name, logo, review, etc. The problem is WordPress generates a URL for this. This isn’t meant to be a page so I just redirect all of my http://website.com/customers/* to somewhere else so that no one goes to these pages (for now).

Is there a way when registering my custom post types in the functions.php file (or some other way) to tell WordPress to not generate a URL / actual page for it? It’s really just a slot to hold my data.

My thoughts were maybe it has to do with:
‘capability_type’ => ‘post’,
or something similar that I’m over looking.

THANKS!

3 s
3

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 Reply

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