I am using post_type_link hook to add a custom taxonomy into the permalink. It works great except now the new permalink just redirects to the landing page – it is not using my single page. If you change the permalink for a custom post type how do you keep the single page working. Also a regular page now links to the landing page as well. I have re-saved the permalinks in the WP admin and no luck. Any ideas?
CODE:
function update_permalink_agendaitem( $post_link, $post, $leavename = true, $sample = false ){
if (get_post_type($post->ID) == 'agendaitem') {
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'agendaitemtype' );
if( $terms ){
return str_replace( '%agendaitemtype%' , get_post_type($post->ID) . '/' . $terms[0]->slug . $post->slug , $post_link );
}
}
} else {
return $post_link;
}
}
add_filter( 'post_type_link', 'update_permalink_agendaitem', 1, 3 );
Then when building the post type:
'rewrite' => array('slug' => '%agendaitemtype%', 'with_front' => false)
2
Answers
UPDATE: so I think what I need to do is reroute to the appropriate single page based on CPT type. I found the code below to do this but it looks like this hook single_template never gets called???
...nothing happens. I've tried with different priorities and still nothing...
So…it seems like. If I try and use the hook post_type_link to update the URL structure (from my first bit of code above), then the single_template hook (my second bit of code) no longer fires for the CPT. If I take out the post_type_link hook callback then the single_template hook fires.