I have a custom post type that I'd like to have a permalink slug based on its taxonomy. All the posts of this CPT have one, and only one, term always marked on this specific taxonomy.
That's my code:

function plugin_domain_register_post_type(){

    add_rewrite_tag('%event_segment%', '([^&]+)');

    register_post_type( 'event',
            'public' => true,
            'rewrite' => array( 'slug' => '%event_segment%'),
            'has_archive' => false,
            'hierarchical' => false,
            'supports' => array( 'title', 'thumbnail')

add_action( 'init', 'plugin_domain_register_post_type' );

function plugin_domain_permalinks($post_link, $post) {
    if (is_object($post) && $post->post_type === 'event') {
        $terms = wp_get_object_terms($post->ID, 'segments');
        if ($terms) {
            return str_replace('%event_segment%' , $terms[0]->slug, $post_link);
    return $post_link;
add_filter('post_type_link', 'plugin_domain_permalinks', 10, 3);

Okay, so it actually works, the permalinks changed and it goes to the right post. However, all the posts and pages (everything that's not of this CPT) that do not have the %event_segment% tag on the slug will literally just display the home page.

I've noticed this happens because of the add_rewrite_tag('%event_segment%', '([^&]+)'); part. The filter doesn't seem to be the issue, since even without the rewrite tag, it works as intended, and modifies the permalinks.

I don't know if this is relevant, but I'm on a Varying Vagrant Vagrants development environment, and it uses nginx, not apache.

What's going on here? Is there something I did wrong or forgot to do?

  • 1
    WordPress determines what to query by the URL pattern- it matches requests against rewrite rules and stops when it finds the first match. Your post type rewrite rules are capturing all of the requests because they are the top-most match. This is why custom post types by default are prefixed with a static string, and tags and categories have a static base, your post type is currently prefixed with something that could contain any value. – Milo Nov 30 '18 at 4:59
  • Thank you so so much! I can't believe it was so simple! May I further ask: There is no way to have a completely dynamic URL like I originally intended, without a static string, right? – Feel The Noise Nov 30 '18 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.