skip to Main Content

I’m building a site but can’t figure out how to change the permalink structure for my custom post type only!

I have a custom post type called "Products" at the moment which has a URL structure like this

/products/continental-rack/

The full code for my custom post type is below:

function cpt_products() {
  $labels = array(
    'name'               => 'Products',
    'singular_name'      => 'Product',
    'menu_name'          => 'Products',
    'name_admin_bar'     => 'Product',
    'add_new'            => 'Add New',
    'add_new_item'       => 'Add New Product',
    'new_item'           => 'New Product',
    'edit_item'          => 'Edit Product',
    'view_item'          => 'View Product',
    'all_items'          => 'All Products',
    'search_items'       => 'Search Products',
    'parent_item_colon'  => 'Parent Product',
    'not_found'          => 'No Products Found',
    'not_found_in_trash' => 'No Products Found in Trash'
  );

  $args = array(
    'labels'              => $labels,
    'public'              => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'show_ui'             => true,
    'show_in_nav_menus'   => true,
    'show_in_menu'        => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-tag',
    'capability_type'     => 'post',
    'taxonomies'          => array( 'category' ),
    'hierarchical'        => false,
    'supports'            => array( 'title', 'author', 'thumbnail' ),
    'has_archive'         => true,
    'rewrite'             => array( 'slug' => 'products' ),
    'query_var'           => true
  );

  register_post_type( 'products', $args );
}
add_action( 'init', 'cpt_products' );

I have enabled the category section on the right side so I can select a category but I cannot get that category to appear within the permalink structure when one is checked.

So the permalink structure I’m aiming for is essentially this:

/%category-name%/products/continental-rack/

Just can’t find a way to do it. Any help would be appreciated!

2

Answers


  1. Go to settings -> permalink scroll down to Product permalinks and select Common Settings and add this in Custom Structure /%taxonomyname%/posttypename/

    enter image description here

    Login or Signup to reply.
  2. You can add filter to post_type_link

    Here is the right way : https://wordpress.stackexchange.com/a/22490

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search