I need help with structuring permalink for the custom post type that I made. I think I tried all the answers here. I'm hoping somebody can help me.

The custom permalink structure in the setting is set as /%category%/%postname%/ and I would like my custom posts' permalink to be the same format. If the custom posts' category is health then its permalink would be localhost/health/post-title. I used post_type_link filter to change the permalink.

enter image description here

And even after re-saving the permalink in the setting, the permalink would lead to 404.

This is how I setup and registered the custom post (in case I set the wrong arguments)

$args = array(
'labels'                => $labels,
'public'                => true,
'exclude_from_search'   => false,
'publicly_queryable'    => true,
'show_ui'               => true,
'show_in_menu'          => true, 
'menu_icon'             => 'dashicons-star-filled',     
'query_var'             => true,                                              
'rewrite'               => array('slug' => 'oa', 'with_front' => false),       
'capability_type'       => 'post',  
'has_archive'           => true,  
'hierarchical'          => false, 
'menu_position'         => 1,   
'supports'              => array('title', 'thumbnail', 'comments'),
'taxonomies'            => array('category', 'post_tag'), 
'show_in_rest'          => true, 
'rest_base'             => 'oa-api',
'rest_controller_class' => 'WP_REST_Posts_Controller',

From what I researched, it seems that I should use add_rewrite_rule method. But I don't understand how rewrite works. Is this the right direction or am I missing something else?

I would like to solve this programmatically, not with a plugin.

  • Did you save permalink again from setting menu? Please try to save it and try to load page again? – Punit Patel May 1 '17 at 14:47
  • Adding a category is fairly simple, you achieve it the same way you are removing the slug, however, you can't add a rewrite rule to support that structure because it already exists- it's the rule that captures parent/child pages. You will create a conflict between these two types, as WordPress has no way of knowing which you are requesting. This is why custom post types have a unique slug by default. – Milo May 1 '17 at 16:28
  • Your WordPress looking for a Page with the slug health. – Brian May 1 '17 at 18:19
  • @PunitPatel I have been saving permalink after after every code change just in case. And still nothing. – Samuel May 1 '17 at 18:53
  • @Milo So you are saying is that I won't be able to remove custom post slug and that custom post slug is necessary if I want /%category%/%postname%/. Am I correct to assume that? So it will always be custom-post/%category%/%postname%/? – Samuel May 1 '17 at 18:58

I cannot post a comment so posting this as an "Answer".

A simple search will lead you to dozens of questions about this in stackoverflow itself, one of which is the following.

Add category base to url in custom post type/taxonomy

Assuming you are trying to get the taxonomy term slug in the URL.

  • To add more info on what I need is that I wish that %category% would be dynamic. If the custom post's category is beauty, then I would like the permalink to be www.domain.com/beauty/post-name/ rather than www.domain.com/oa/beauty/post-name. I tried that solution before. Basically I do not want the custom post slug anywhere in the permalink. But from what I researched, that seems impossible as of now and I might have to create rewrite rule? – Samuel May 2 '17 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.