Give a permalink to Custom Post Types without title

I have a “report” Custom Post Type registered in this way:

register_post_type(
    'report',
    array(
        'label' => 'Reports',
        'description' => '',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => ''),
        'query_var' => true,
        'exclude_from_search' => false,
        'supports' => array('custom-fields','comments','author',),
        ...
    )
);

As you can see, my report does not support title, but only custom-fields, comments, and author:

Read More
'supports' => array('custom-fields','comments','author',)

Hovewer, since the permalink of a post normally is something like:

http://mywebsitename.com/report/the-title

now I always get something like:

 http://mywebsitename.com/report/auto-draft/

I suppose that “auto-draft” is the title that is given by default to my report. I have no idea how to give a reasonable permalink. I would like to give one based on the custom-fields.

Related posts

Leave a Reply

1 comment

  1. I have no idea how to give a reasonable permalink.
    

    Give it a title. You don’t have to use the title for anything but the permalink, and it will be used in the page title as well most likely but since it is already exposed in the permalink I don’t see the harm. You never have to print it anywhere else if you don’t want to. Not to be ornery, but that is the obvious solution and then everything more or less falls into place.

    ‘title’ is not something you can easily opt out of. How do you identify the reports on the “edit” screen for example? Those should probably all say “auto-draft” too, right? Or something pretty close? The page title– on this page it looks like “plugin development – Give a permalink to …”– is usually set to the post title or derived from it. That bit shows up in most search engine results so you really need a decent title there if this is going to be exposed to the public. If you weren’t looking to have the permalinks it would be different. You’d need to write your own interface to view the reports but at least some of the problems would not occur.

    If you really want to, you could probably set the slug manually by hooking into the save_post filter and that might get a decent permalink. I don’t think it will help with the admin screen though or with the page title. You should also be able use the slug the slug in the admin edit panel, but that is more code to write.

    Please reconsider your decision not to use the title. That decision is costing you a fair bit of work, and I can’t see the benefit.