1

I have defined the following custom post type + taxonomy

$productTaxonomyLabels = [
        'name' => __('Product Categories', 'byronposttypes'),
        'singular_name' => __('Product Category', 'byronposttypes'),
    ];
    $productTaxonomyArgs = [
        'labels' => $productTaxonomyLabels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_admin_column' => true,
        'description' => __('Product category', 'byronposttypes'),
        'hierarchical' => true,
        'rewrite' => [
            'slug' => __('collections', 'byronposttypes'),
            'with_front' => false,
            'hierarchical' => true,
            'ep_mask' => EP_CATEGORIES
        ],
    ];
    register_taxonomy('collections', 'products', $productTaxonomyArgs);
    register_taxonomy_for_object_type('collections', 'products');

    $productLabels = [
    'name' => _x('Products', 'post type plural name', 'byronposttypes'),
    'singular_name' => _x('Product', 'post type singular name', 'byronposttypes'),
    'menu_name' => _x('Collection', 'name in admin menu', 'byronposttypes'),
    'add_new' => _x('Add new', 'product', 'byronposttypes'),
    'add_new_item' => __('Add new product', 'byronposttypes'),
    'new_item' => __('New product', 'byronposttypes'),
    'edit_item' => __('Edit product', 'byronposttypes'),
    'view_item' => __('View product', 'byronposttypes'),
    'not_found' => __('No products found', 'byronposttypes'),
    'not_found_in_trash' => __('No products found in trash', 'byronposttypes'),
    'all_items' => __('All products', 'byronposttypes'),
    'archives' => __('Product archives', 'byronposttypes'),
    'attributes' => __('Product attributes', 'byronposttypes'),
];

$productSupports = ['title', 'editor', 'thumbnail'];

$productArgs = [
    'labels' => $productLabels,
    'description' => __('Products', 'byronposttypes'),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'menu_position' => 57,
    'menu_icon' => 'dashicons-screenoptions',
    'capability_type' => ['product', 'products'],
    'hierarchical' => false,
    'supports' => $productSupports,
    'taxonomies' => ['banaan'],
    'rewrite' => [
        'slug' => 'banaan',
        'with_front' => false,
    ],
    'has_archive' => 'banaan',
];
register_post_type('products', $productArgs);

As you can see, I have defined the post type slug to be 'banaan'. The problem is, that the link wordpress generates (for instance, when clicking 'view' in wp-admin, the uri is collection/$product_name. I used to have /collection as base of the url, but I changed that to banaan for testing purposes, but it won't change. I have re-saved the permalinks. Am I missing something here?
*Manually navigating to /banaan/$product_name redirects to /collection/$product_name and gives a 404

2 Answers 2

0

Try setting your 'rewrite' in the CPT to:

'rewrite' => false,

save permalinks.
check frontside of site to ensure it worked. (if it doesn't you may have a duplicate code somewhere...)

then set it back to:

'rewrite' => (
    'slug' => 'banaan',
    'with_front' => false,
),

flush permalinks again.
Smile (hopefully)

Why have you elected to use brackets instead of parentheses? You might try using the traditional method.

2
  • I am very sorry for my late answer, I did not have the chance to try it out up until now. Setting rewrite to false does make wordpress change the permalink, but setting it back to 'slug' => 'banaan' makes the base 'collection' again for some reason, so the it does not fix the issue. I opt to use [] because this is just the shorthand notation for array(). () is not a valid syntax.
    – Connor
    Apr 13, 2017 at 7:17
  • When I add a new product, it shows the sample permalink to be /banaan/product-name, but as soon as I save the product, it changes the permalink to /collection/product-name.
    – Connor
    Apr 13, 2017 at 7:38
0

I seem to have solved the issue. I renamed my CPT from products to product

$productTaxonomyLabels = [
    'name' => __('Product Categories', 'byronposttypes'),
    'singular_name' => __('Product Category', 'byronposttypes'),
];
$productTaxonomyArgs = [
    'labels' => $productTaxonomyLabels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_admin_column' => true,
    'description' => __('Product category', 'byronposttypes'),
    'hierarchical' => true,
    'rewrite' => [
        'slug' => __('collections', 'byronposttypes'),
        'with_front' => false,
        'hierarchical' => true,
        'ep_mask' => EP_CATEGORIES
    ],
];
register_taxonomy('collections', 'product', $productTaxonomyArgs);
register_taxonomy_for_object_type('collections', 'product');

$productLabels = [
    'name' => _x('Products', 'post type plural name', 'byronposttypes'),
    'singular_name' => _x('Product', 'post type singular name', 'byronposttypes'),
    'menu_name' => _x('Collection', 'name in admin menu', 'byronposttypes'),
    'add_new' => _x('Add new', 'product', 'byronposttypes'),
    'add_new_item' => __('Add new product', 'byronposttypes'),
    'new_item' => __('New product', 'byronposttypes'),
    'edit_item' => __('Edit product', 'byronposttypes'),
    'view_item' => __('View product', 'byronposttypes'),
    'not_found' => __('No products found', 'byronposttypes'),
    'not_found_in_trash' => __('No products found in trash', 'byronposttypes'),
    'all_items' => __('All products', 'byronposttypes'),
    'archives' => __('Product archives', 'byronposttypes'),
    'attributes' => __('Product attributes', 'byronposttypes'),
];

$productSupports = ['title', 'editor', 'thumbnail'];

$productArgs = [
    'labels' => $productLabels,
    'description' => __('Products', 'byronposttypes'),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'menu_position' => 57,
    'menu_icon' => 'dashicons-screenoptions',
    'capability_type' => ['product', 'products'],
    'hierarchical' => false,
    'supports' => $productSupports,
    'taxonomies' => ['collections'],
    'rewrite' => [
        'slug' => __('product', 'byronposttypes'),
        'with_front' => false,
        'ep_mask' => EP_PERMALINK
    ],
    'has_archive' => false,
];
register_post_type('product', $productArgs);

I still do not know what was causing the problem, but changing the name of the CPT seems to fix it.

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.