Custom Post Type Permalink

admin2025-01-07  5

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.

Share Improve this question edited Aug 2, 2019 at 22:02 Maher Aldous 1194 bronze badges asked May 1, 2017 at 14:35 SamuelSamuel 112 bronze badges 5
  • Did you save permalink again from setting menu? Please try to save it and try to load page again? – Punitkumar Patel Commented May 1, 2017 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 Commented May 1, 2017 at 16:28
  • Your WordPress looking for a Page with the slug health. – somebodysomewhere Commented May 1, 2017 at 18:19
  • @PunitPatel I have been saving permalink after after every code change just in case. And still nothing. – Samuel Commented May 1, 2017 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 Commented May 1, 2017 at 18:58
Add a comment  | 

3 Answers 3

Reset to default 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.

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264507a973.html

最新回复(0)