0

I have a WP website where a created a CPT named product, which has several custom fields, including a category field. This "category" is a custom taxonomy that can take several values, let's say : category1 or category2 or category3. The website has also "regular posts" : articles, pages...

I'd like to have, for each product, this kind of url : mysite.com/category1/product1name

instead of

mysite.com/product/category1/product1name

I tried to play around with the "rewrite" property of the custom post "product" but I couldn't acheive my goal. I'll talk about this later. For now, this property is disabled.

Then I wrote this piece of code (produit=product, categorie-produit=product-category) :

add_filter('post_type_link', 'produit_type_permalink', 10, 4);

function produit_type_permalink($post_link, $post, $leavename, $sample) {

     // get the current post
     global $post;

    //if the post is a Product custom post
    if ($post->post_type == 'produit') {

        // retrieve the product category which is a custom taxonomy
        $terms = get_the_terms($post->id, 'categorie-produit');

        // testing if not empty
        if (!empty($terms[0])) {

            //retrieve the category name
            $term = $terms[0]->slug;

            //rewriting the permalink with the appropriate term
            $permalink = str_replace('/produit/', "/$term/", $post_link);

            //display, for testing purpose
            echo $permalink;

            return $permalink;

        }

    }

    // return the link without changing anything
    else {

        return $post_link;

    }   
}

This function behaves strangely : when I try to display a custom product, its URL is still mysite.com/product/category1/product1name, but the URL echoed "for testing purpose" has the correct form :

mysite.com/category1/product1name

Why this function is not taken into account ? Is there a problem with its priority ? Is there a better way to proceed ?

If I use the "rewrite" property for my product custom post, like this :

'rewrite' => array(
            'slug' => '/%categorie-produit%',
            'with_front' => false
        ),

then I modify the function above :

$permalink = str_replace('%categorie-produit%/', "/$term/", $post_link);

The URL rewriting works properly for the products, obviously the function is taken into account, but I can't display the articles and the pages anymore, that is to say : the regular posts ! And this problem is the same with the other custom posts types I created. In all those case, I get a 404 error as if the rewriting of the URL for the products had a side effect on the other post types.

Could someone explain me where I am wrong ?

Thanks for your help

4
  • I think this topic can help.
    – Minh Tri
    Commented Apr 24, 2016 at 8:39
  • thanks I already saw it but I can't find how to use their solution for my case. What I have now is more or less similar to wordpress.stackexchange.com/questions/5308/…
    – Fafanellu
    Commented Apr 24, 2016 at 10:26
  • The simplest fix for this is to just use the default permalinks with product in the URL. This is how WordPress knows you are requesting a product when you visit those URLs. When you make the URL match the same structure as another post type, WordPress can no longer distinguish between the two types of requests, one rule will always "win" over the other. WordPress has to know what kind of thing it's looking for before it tries to find it in the database.
    – Milo
    Commented Apr 24, 2016 at 14:16
  • It's what I do for now, otherwise the "regular" posts aren't found anymore
    – Fafanellu
    Commented Apr 24, 2016 at 16:27

0

Your Answer

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