1

Here is the code that I modified from https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/

/*
* Creating a function to create our CPT
* just drop this in functions.php inside your theme file basically...
* You replaced all 'twentytwenty' with 'twentyseventeen'
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Products', 'Post Type General Name', 'twentyseventeen' ),
        'singular_name'       => _x( 'Product', 'Post Type Singular Name', 'twentyseventeen' ),
        'menu_name'           => __( 'Products', 'twentyseventeen' ),
        'parent_item_colon'   => __( 'Parent Product', 'twentyseventeen' ),
        'all_items'           => __( 'All Products', 'twentyseventeen' ),
        'view_item'           => __( 'View Product', 'twentyseventeen' ),
        'add_new_item'        => __( 'Add New Product', 'twentyseventeen' ),
        'add_new'             => __( 'Add New', 'twentyseventeen' ),
        'edit_item'           => __( 'Edit Product', 'twentyseventeen' ),
        'update_item'         => __( 'Update Product', 'twentyseventeen' ),
        'search_items'        => __( 'Search Product', 'twentyseventeen' ),
        'not_found'           => __( 'Not Found', 'twentyseventeen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentyseventeen' ),
    );

// Set other options for Custom Post Type

    $args = array(
        //'label'               => __( 'products', 'twentyseventeen' ),
        'description'         => __( 'Products and reviews', 'twentyseventeen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'category', 'tag' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        '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'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,

    );

    // Registering your Custom Post Type
    register_post_type( 'products', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

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

I created a category called "shop" and I want these CPT "products" to display in my "shop" category so that when you visit domain.com/shop you see a list of them.

I have added "category" capability to taxonomies, and I can select the shop category when creating a new product, but the permalink is always only ever domain.com/products/product-name

Whenever I create a new product custom post, I fill out the description, add the custom fields, title, select the shop category... and everything seems fine until I press publish... then a "permalink" dropdown section appears within the editor, and I am told that I can see the product at domain.com/products/product-name ... when I never selected a products category... and a products category doesn't even exist/never existed!!!

What gives?

If i visit domain.com/shop there is nothing to be seen.

And if I visit domain.com/shop/product-name I am redirected to domain.com/products/product-name (which displays fine by the way).

Any idea where this ghost category called "products" is originating from?

Why is the editor not respecting my desire to place a product in the "shop" category? And instead placing it in it's own "products" category (that doesn't even exist/was never created inside the wordpress admin dashboard).

The only edit I have made to any php code is adding the single function inside my theme's functions.php file.

I even updated/flushed the permalinks settings by re-saving on the custom permalinks section of the dashboard.

Thank you.

EDIT: I believe I may be missing the "rewrite" arg? As explained here: https://developer.wordpress.org/reference/functions/register_post_type/

'rewrite'
(bool|array) Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be passed with any of these keys:

1 Answer 1

4

This is the normal behaviour of a custom post type. Any post type other than pages and posts has the post type name in the URL. So for this URL:

https://example.com/products/product-name/

"products" is the post type. Not the category. Similarly, when registering a post type, if has_archive is set to true, then all Products will be automatically viewable at:

https://example.com/products/

The rewrite and has_archive properties can be used to change which slug is used for each for these. For example, if you set:

'has_archive' => 'shop',
'rewrite'.    => [
    'slug'       => 'product',
    'with_front' => false,
],

Then the products archive URL will be:

https://example.com/shop/

And the individual products' URL will be:

https://example.com/product/product-name/

In neither of these cases do you need a "product" or "shop" category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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