php - Wordpress URL Rewrite to sync custom taxonomy and post type


Here are my current URLs for my custom post type and taxonomy. I noticed the rewrite slug for the custom post type and taxonomy cannot both be "inventory" or it breaks. So i went with "inventory-category" and "inventory".

current urls:

category http://localhost:3000/inventory-category/actual-category-name/

single http://localhost:3000/inventory/product-title

I want:

category http://localhost:3000/inventory/actual-category-name/

single http://localhost:3000/inventory/actual-category-name/product-title

So I have 2 questions

  1. How do I get the post type and taxonomy to both share the same url "inventory" instead of using "inventory-category".

  2. How do I get the single to use the actual category name? So it could be:



// register a new list of categories for custom post type
function so_inventory_categories() {
    $args = array(
        'hierarchical'       => true,
        'show_ui'            => true,
        'show_admin_column'  => true,
        'query_var'          => true,
        'public'             => true,
        'publicly_queryable' => true,
        'rewrite'            => array('with_front' => false, 'slug' => 'inventory-category'),

    register_taxonomy( 'inventory_categories', array( 'inventory' ), $args );
add_action( 'init', 'so_inventory_categories' );

function so_posttype_inventory() {
    $args = array(
        'labels'              => ['name'=>'Inventory'],
        'supports'            => array( 'title', 'editor' ),
        'taxonomies'          => array( 'inventory_categories' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite'             => array('with_front' => false,'slug' => 'inventory'),
        'menu_icon'           => 'dashicons-plus-alt',
    register_post_type( 'inventory', $args );

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



You should use rewrite rules to make that work. Something like this:

add_action('init', 'custom_rewrite_basic');
function custom_rewrite_basic() {
    add_rewrite_rule('^inventory/([^/]+)/?$', 'index.php?taxonomy=inventory_category&inventory_category=$matches[1]', 'top');
    add_rewrite_rule('^inventory/([^/]+)/([^/]+)/?$', 'index.php?post_type=inventory&inventory=$matches[2]', 'top');

I haven't tested this so it might still need some tweaking.

People are also looking for solutions to the problem: php - When How do I split an array into two or more arrays with almost equal sum?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.