My custom permalink with dynamic taxonomy is working for my custom post type. However, it breaks all my other permalinks. They display a 404 error in the content area (header and sidebar still display).

I used the following code to create the dynamic permalinks for the custom post type:

/*Adds Custom Permalinks for Course Segments*/
function custom_post_link($post_link, $id = 0)
  $post = get_post($id);

  if(!is_object($post) || $post->post_type != 'course-segment')
    return $post_link;
  $course = 'course-segment';

  if($terms = wp_get_object_terms($post->ID, 'course'))
    $course = $terms[0]->slug;
  return str_replace('%course%', $course, $post_link);

  return $post_link;

add_filter('post_type_link', 'custom_post_link', 1, 3);

I also added the following to my create_post_type() function:

'rewrite' => array('slug' => '%course%')

I got the code to do this from: https://stackoverflow.com/questions/7723457/wordpress-custom-type-permalink-containing-taxonomy-slug.

By simply commenting out the following two lines of code my old permalinks work, but of course my dynamic permalinks don't:

//add_filter('post_type_link', 'custom_post_link', 1, 3);

//'rewrite' => array('slug' => '%course%')

In Settings my permalinks are set to %postname%.

Any thoughts or insights on how I can get both my normal permalinks and my dynamic custom post type permalinks working would be most appreciated! -Mark

2 Answers 2


If you're running WordPress 3.0.1 or later, I believe your problem lies with the 'post_type_link' filter declaration and function arguments.

When the 'post_type_link' filter is applied, it passes the following 4 arguments:

apply_filters('post_type_link', $post_link, $post, $leavename, $sample);

But your function accepts $post_link and $id.

Try the following adjustments:

function custom_post_link( $post_link, $post ) {

    if ( $post->post_type != 'course-segment')
        return $post_link;

    $course = 'course-segment';
    if( $terms = wp_get_object_terms( $post->ID, 'course' ) )
        $course = $terms[0]->slug;

    return str_replace( '%course%', $course, $post_link );

add_filter( 'post_type_link', 'custom_post_link', 1, 2 );
  • Thank you for your response. Unfortunately, it is giving me the same results: course segments work, but all other posts are 404. I'm using Wordpress 3.3.1 and my theme is Canvas 4.7.9 with the latest Woo Framework. Any other thoughts? Or any other info I can provide to help get to the bottom of this? Thanks again! Mar 17, 2012 at 15:34
  • Are you sure the problem lies with this filter? With the exception of return $post_link;, comment out the rest of the custom_post_link() function (or use what I've pasted below), flush your rewrite rules (save changes on your permalink settings page), and then see if you're still having the 404 problem. function custom_post_link( $post_link, $post ) { return $post_link; } add_filter( 'post_type_link', 'custom_post_link', 1, 2 ); Mar 17, 2012 at 19:32

Rachel is right. The problem is not the filter. Removing it completely the issue still happens because of the %taxonomy% (with the percent symbols) as your custom post type slug.

I tried using a non-tag (without the percent symbols) and everything else works, but then I can't replace it completely with the taxonomies - i.e. it's not catched by the same filter.

So the replacement is happening somehow else I can't understand.

I can't have the CPTs showing the taxonomy in the permalink AND the rest of the posts/pages working at the same time. It's one or the other.

I think it has something to do with wp looking for taxo/postname BEFORE page/subpage or not trying both before to return a 404, but it's too advanced for me.


global $wp_rewrite;
$wp_rewrite->extra_permastructs['listing'][0] = "%listing_type%/%postname%";
    add_rewrite_rule("([^/]+)/([^/]+)/?$", 'index.php?post_type=listing&listing_type=$matches[1]&name=$matches[2]', 'bottom');
    add_rewrite_rule("([^/]+)/([^/]+)/?$", 'index.php?post_type=listing&listing_type=$matches[1]&page=$matches[2]', 'bottom');

The above code (inside the CPT definition function) only fixed the posts, but the pages (what the second add_rewrite_rule line was ment for) are still broken.

My details are here.

Dynamic taxonomy in permalink made all other posts NOT FOUND? Unfortunatelly I didn't found this question before or I hadn't posted mine.

Any idea will be appreciated

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.