1

I have added a custom post type, I want to add the custom post category in its permalink instead of the static base.

For example for the custom posts I want the following permalink structure:

mysite.com/category1/custom-post1
mysite.com/category3/custom-post2
mysite.com/category4/custom-post3

In the above examples category1, category2 and category3 are the categories of custom post type.

Notice that there is no custom post base slug in the URL.

I am using the following code for it, it works for the custom posts and gets the permalinks correctly, however it gives 404 error for the default posts and pages. How can I fix that so that default posts and pages also work?

Default page/post permalink would be like:

mysite.com/sample-page
mysite.com/2021/04/hello-world/

Here's the code that I'm using:

register_post_type( 'business', array( 
    'labels'                 => array(
        'name'               => __( 'Businesses' ),
        'singular_name'      => __( 'Business' ),
        'add_new'            => __( 'Add New' ),
        'add_new_item'       => __( 'Create New Business' ),
        'edit'               => __( 'Edit' ),
        'edit_item'          => __( 'Edit Business' ),
        'new_item'           => __( 'New Business' ),
        'view'               => __( 'View Businesses' ),
        'view_item'          => __( 'View Business' ),
        'search_items'       => __( 'Search Businesses' ),
        'not_found'          => __( 'No businesses found' ),
        'not_found_in_trash' => __( 'No businesses found in trash' ),
        'parent'             => __( 'Parent Business' ),
    ),
    'description'           => __( 'This is where you can create new businesses on your site.' ),
    'public'                => true,
    'show_ui'               => true,
    'capability_type'       => 'post',
    'publicly_queryable'    => true,
    'exclude_from_search'   => false,
    'has_archive'           => true,
    'menu_position'         => 2,
    'menu_icon'             => 'dashicons-id',
    'hierarchical'          => true,
    '_builtin'              => false,
    'rewrite'               => array( 'slug' => '%business_cat%', 'with_front' => false ),
    'query_var'             => true,
    'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
) );


add_action( 'init', 'create_product_taxonomies', 0 );

function create_product_taxonomies() {
    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      =>  __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Categories' ),
        'parent_item_colon' => __( 'Parent Categories:' ),
        'edit_item'         => __( 'Edit Category' ), 
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Category' ),
    );  

    register_taxonomy( 'business_cat', array( 'business' ), array(
        'hierarchical'  => true,
        'labels'        => $labels,
        'show_ui'       => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' => '%business_cat%', 'with_front' => false ),
    ) );
}

function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === 'business' ) {
        if ( $terms = get_the_terms( $post->ID, 'business_cat' ) )
            $link = str_replace( '%business_cat%', current( $terms )->slug, $link );
        else
            $link = str_replace( '%business_cat%', 'uncategorized', $link );
    }

    return $link;
}

add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );

Your Answer

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

Browse other questions tagged or ask your own question.