If you are using this in a plugin, from the codex:
To get permalinks to work when you activate the plugin use the
following example, paying attention to how my_cpt_init is called in
the register_activation_hook callback:add_action( 'init', 'my_cpt_init' ); function my_cpt_init() { register_post_type( ... ); } function my_rewrite_flush() { // First, we "add" the custom post type via the above written function. // Note: "add" is written with quotes, as CPTs don't get added to the DB, // They are only referenced in the post_type column with a post entry, // when you add a post of this CPT. my_cpt_init(); // ATTENTION: This is *only* done during plugin activation hook in this example! // You should *NEVER EVER* do this on every page load!! flush_rewrite_rules(); } register_activation_hook( __FILE__, 'my_rewrite_flush' );
I would place
// add to our plugin init function
global $wp_rewrite;
$event_structure="/events/%event_year%/%event%";
$wp_rewrite->add_rewrite_tag("%event%", '([^/]+)', "event=");
$wp_rewrite->add_permastruct('event', $event_structure, false);
In a separate action than the post registration, but before the flush.
Is the return $permalink
output correct?