$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - add_rewrite_tag broke permalinks that doesn't use that specific tag|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - add_rewrite_tag broke permalinks that doesn't use that specific tag

matteradmin9PV0评论

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',
        array(
            '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?

Post a comment

comment list (0)

  1. No comments so far