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

function my_custom_init()
{

    register_taxonomy('manufacturer', ['product'], [
        'labels' => [
            'name' => 'Manufacturer',
            'singular_name' => 'Manufacturer',
        ],

        'rewrite' => ['slug' => '/']
    ]);

    register_post_type('product', [
        'labels' => [
            'name' => 'Products',
            'singular_name' => 'Product'
        ],

        'public' => true,
        'menu_position' => 5,
        'supports' => ['title', 'editor', 'excerpt', 'thumbnail', 'comments'],
        'taxonomies' => ['manufacturer'],
        'rewrite' => ['slug' => '%manufacturer%']
    ]);

}


add_filter('post_type_link', 'wpa_product_permalinks', 1, 2);

function wpa_product_permalinks($post_link, $post)
{

    if (is_object($post) && $post->post_type == 'manufacturer') {
        $terms = wp_get_object_terms($post->ID, 'manufacturer');

        if ($terms) {
            return str_replace('%manufacturer%', $terms[0]->slug, $post_link);
        }
    }

    return $post_link;
}

Examples:

domain.com/samsung/galaxy-s10-plus/ - open product 'Galaxy S10 Plus'

domain.com/samsung/ - open manufacturer 'Samsung'

domain.com/about-us/- open 'Not found 404'.

How to fix?

But after this does not open normal pages such as '/about-us/' and others. How to do to avoid the 'not found 404' error for normal pages and keep the desired structure domain.com/%manufacturer%/%postname%/ for custom post type 'products' and custom taxonomy 'manufacturer'?

Your Answer

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

Browse other questions tagged or ask your own question.