I know people have asked this before and have gone as far as adding the custom post type, and rewrite for permalink.
The problem is I have 340 existing categories which I would like to continue using. I used to be able to see /category/subcategory/postname
Now I have the slug of customposttype/postname. Selecting the category no longer shows up in permalink…I’ve not changed the permalink setting in admin to anything different.
Is there something I’m missing or need to add to this code?
function jcj_club_post_types() {
register_post_type( 'jcj_club', array(
'labels' => array(
'name' => __( 'Jazz Clubs' ),
'singular_name' => __( 'Jazz Club' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Jazz Club' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Jazz Clubs' ),
'new_item' => __( 'New Jazz Club' ),
'view' => __( 'View Jazz Club' ),
'view_item' => __( 'View Jazz Club' ),
'search_items' => __( 'Search Jazz Clubs' ),
'not_found' => __( 'No jazz clubs found' ),
'not_found_in_trash' => __( 'No jazz clubs found in Trash' ),
'parent' => __( 'Parent Jazz Club' ),
),
'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'menu_position' => 5,
'query_var' => true,
'supports' => array(
'title',
'editor',
'comments',
'revisions',
'trackbacks',
'author',
'excerpt',
'thumbnail',
'custom-fields',
),
'rewrite' => array( 'slug' => 'jazz-clubs-in', 'with_front' => true ),
'taxonomies' => array( 'category','post_tag'),
'can_export' => true,
)
);
There are 2 points of attack to cover when you are adding custom post type rewrite rules:
Rewrite rules
This happens when the rewrite rules are being generated in
wp-includes/rewrite.php
inWP_Rewrite::rewrite_rules()
. WordPress allows you to filter the rewrite rules for specific elements like posts, pages and various types of archive. Where you seeposttype_rewrite_rules
theposttype
part should be the name of your custom post type. Alternatively you can use thepost_rewrite_rules
filter as long as you don’t obliterate the standard post rules too.Next we need the function to actually generate the rewrite rules:
The main thing to watch out for here if you decide to play around is the ‘Walk directories’ boolean. It generates rewrite rules for each segment of a permastruct and can cause rewrite rule mismatches. When a WordPress URL is requested the rewrite rules array is checked from top to bottom. As soon as a match is found it will load whatever it has come across so for example if your permastruct has a greedy match eg. for
/%category%/%postname%/
and walk directories is on it will output rewrite rules for both/%category%/%postname%/
AND/%category%/
which will match anything. If that happens too early you’re screwed.Permalinks
This is the function that parses the post type permalinks and converts a permastruct (eg ‘/%year%/%monthnum%/%postname%/’) into an actual URL.
The next part is a simple example of what would ideally be a version of the
get_permalink()
function found inwp-includes/link-template.php
. Custom post permalinks are generated byget_post_permalink()
which is a much watered down version ofget_permalink()
.get_post_permalink()
is filtered bypost_type_link
so we’re using that to make a custom permastructure.As mentioned this a very simplified case for generating a custom rewrite ruleset and permalinks, and is not particularly flexible but it should be enough to get you started.
Cheating
I wrote a plugin that lets you define permastructs for any custom post types, but like you can use
%category%
in the permalink structure for posts my plugin supports%custom_taxonomy_name%
for any custom taxonomies you have too wherecustom_taxonomy_name
is the name of your taxonomy eg.%club%
.It will work as you’d expect with hierarchical/non-hierarchical taxonomies.
http://wordpress.org/extend/plugins/wp-permastructure/
I have found a SOLUTION!!!
(After endless research.. I can have CUSTOM POST TYPE permalinks like:
example.com/category/sub_category/my-post-name
here the code (in functions.php or plugin):
Got the solution!
To have hierarchical permalinks for custom post type install Custom Post Type Permalinks(https://wordpress.org/plugins/custom-post-type-permalinks/) plugin.
Update registered post type. I have post type name as help center
And here is registered taxonomy
This is line makes your permalink work
you can remove
%post_id%
and can keep/%help_centre_category%/%postname%/"
Don’t forget to flush permalinks from dashboard.
You have several errors with your code. I cleaned up your existing code:
Replace your code with the code above and see if it works. Reply back if you have further questions and I’ll try to help.
EDIT:
I noticed I left out
'has_archive' => true
.