0

I have created 2 x Wordpress Custom Post Types

One is called Category, the other is Products and the products have the categories as the parent with the code below:

Products

function my_custom_post_product() {
$labels = array(
    'name'               => _x( 'Products', 'post type general name' ),
    'singular_name'      => _x( 'Product', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Product' ),
    'edit_item'          => __( 'Edit Product' ),
    'new_item'           => __( 'New Product' ),
    'all_items'          => __( 'All Products' ),
    'view_item'          => __( 'View Product' ),
    'search_items'       => __( 'Search Products' ),
    'not_found'          => __( 'No products found' ),
    'not_found_in_trash' => __( 'No products found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Products'
);
$args = array(
    'labels'        => $labels,
    'hierarchical' => true,
    'description'   => 'Holds our products and product specific data',
    'public'        => true,
    'publicly_queryable' => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments','page-attributes' ),
    'has_archive'   => true,
    'query_var' => true,
    'rewrite' => array(
        'slug' => '',
        'with_front' => false,
    )


);
register_post_type( 'product', $args );

}
add_action( 'init', 'my_custom_post_product' );

Categories

function my_custom_post_category() {
$labels = array(
    'name'               => _x( 'Categories', 'post type general name' ),
    'singular_name'      => _x( 'Category', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Category' ),
    'edit_item'          => __( 'Edit Category' ),
    'new_item'           => __( 'New Category' ),
    'all_items'          => __( 'All Categories' ),
    'view_item'          => __( 'View Category' ),
    'hierarchical' => true,
    'search_items'       => __( 'Search Categories' ),
    'not_found'          => __( 'No categories found' ),
    'not_found_in_trash' => __( 'No categories found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Categories'
);
$args = array(
    'labels'        => $labels,
    'description'   => 'Category Pages',
    'hierarchical' => true,
    'public'        => true,
    'publicly_queryable' => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments','post-formats','custom-fields','page-attributes' ),
    'has_archive'   => true,
    'can_export' => true,
    'query_var' => 'category',

);
register_post_type( 'category', $args );

}

add_action( 'init', 'my_custom_post_category' );

The issue I am having is with the permalinks, at the minute they are like this:

Category - http://domain.com/category/category-name/ Product - http://domain.com/product/category-name/product-name/

I would want to make it like this:

Category - http://domain.com/category-name/ Product - http://domain.com/category-name/product-name/

I have tried various things but it just ends up giving a 404 even after refreshing permalink settings.

  • This question has been asked here many times, if you search around you will find possible solutions. – Milo Jan 21 '18 at 14:14
  • I have tried a few things from other articles, the usual answer is to change the permalink options in the array and then just click save on the permalink options but I cant seem to find anything that works. – user2572639 Jan 21 '18 at 16:08
0

I don't recommend using category as cpt try rename it for example in product-type. For more information on category see on codex: Taxonomies

function my_custom_post_product_type() {

    $labels = array(
        'name'               => _x( 'Product Types' ),
        'singular_name'      => _x( 'Product Type' ),
        'add_new'            => _x( 'Add New' ),
        'add_new_item'       => __( 'Add New Product Type' ),
        'edit_item'          => __( 'Edit Product Type' ),
        'new_item'           => __( 'New Product Type' ),
        'all_items'          => __( 'All Product Types' ),
        'view_item'          => __( 'View Product Type' ),
        'hierarchical'       => true,
        'search_items'       => __( 'Search Product Types' ),
        'not_found'          => __( 'No Product Types found' ),
        'not_found_in_trash' => __( 'No Product Types found in the Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Product Types'
    );

    $args = array(
        'labels'             => $labels,
        'description'        => 'Product Type Pages',
        'hierarchical'       => true,
        'public'             => true,
        'publicly_queryable' => true,
        'menu_position'      => 5,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments','post-formats','custom-fields','page-attributes' ),
        'has_archive'        => true,
        'can_export'         => true,
        'query_var'          => 'product-type'
    );

    register_post_type( 'product-type', $args );
}
add_action( 'init', 'my_custom_post_product_type' );

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.