1

Re: Custom post types, taxonomies, and permalinks by @TheDeadMechanic The solution above works perfectly, and rewrite is great, but can you advise on how to rewrite for more than one custom post type with its own taxonomy? the code below works for taxonomy product cat under products, but adding another CPT with taxonomy gives 404 as it not included below, how can the code below be amended to cater for more than one CPT and taxonomy?

    function filter_post_type_link($link, $post){
    if ($post->post_type != 'product_listing')
        return $link;

    if ($cats = get_the_terms($post->ID, 'product_cat'))
        $link = str_replace('%product_cat%', array_pop($cats)->slug, $link);
    return $link;
    }
 add_filter('post_type_link', 'filter_post_type_link', 10, 2);

thanks in advance

1
  • I am having the same struggle, getting 404 error with more than one rewrite of custom taxonomies or pst types -.- Hope someone can help. Jun 22, 2012 at 15:30

1 Answer 1

1

Assuming the code snippet provided works then extending the conditional statement like so will help you,

function filter_post_type_link($link, $post)
{
    if ($post->post_type = 'custom_post_type_1') {

        if ($cats = get_the_terms($post->ID, 'custom_cat_1'))

             $link = str_replace('%custom_cat_1%', array_pop($cats)->slug, $link);

        return $link;

    } elseif ($post->post_type = 'custom_post_type_2') {

        if ($cats = get_the_terms($post->ID, 'custom_cat_2'))

             $link = str_replace('%custom_cat_2%', array_pop($cats)->slug, $link);

        return $link;

    } else {

        return $link;

    }

}

add_filter('post_type_link', 'filter_post_type_link', 10, 2);

Your Answer

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

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