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/oa/beauty/post-name
but I want it to looks like this www.domain/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.
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.
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.
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.
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.
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
And the Woocommerce have it's own custom post type which is product. It can be found here if you download WooCommerce Plugin
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.
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
health
. – somebodysomewhere Commented May 1, 2017 at 18:19/%category%/%postname%/
. Am I correct to assume that? So it will always becustom-post/%category%/%postname%/
? – Samuel Commented May 1, 2017 at 18:58