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

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

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

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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