skip to Main Content

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


  1. Chosen as BEST ANSWER

    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???

      function update_single_templatee( $single_template ) {
        wp_die("WHEN DOES THIS FIRE???");
      }
      add_filter( 'single_template', 'update_single_template', 11 );
    

    ...nothing happens. I've tried with different priorities and still nothing...


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search