1

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.

More Info

The custom post is looks like this in my url www.domain.com/oa/beauty/post-name but I want it to looks like this www.domain.com/beauty/post-name/ The beauty is an example of the category name.

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

In my custom post type I tried to rename the url and then saved it but when I visit it the permalink (url) is leading to 404. Even after re-saving the permalink in the settings, the problem still there.

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? – Punitkumar 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
0

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
0

Sorry I can't comment but I want to clarify something about Custom Post Types. This is not an full answer so please edit it if I am wrong.

The Wordpress have it's own post which is the standard post. It can be found here enter image description here

And the Woocommerce have it's own custom post type which is product. It can be found here if you download WooCommerce Plugin enter image description here

So every advanced custom post type have thier own categories and tags as you can see in the images. So have you checked if you have the same menu type like your own category in your custom. Because if your own custom post type doesn't have it's own category and tag menu it will be hard to get the category you want fromt wordpress standard post. It's like you take a category from another post type or from a default post type.

0

In the past I have made it with https://developer.wordpress.org/reference/functions/add_rewrite_rule/

The best solution, with a simple regexp can read and detect all parameters in your URL and rewrite to show the correct post

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.