Custom Permalink Structure for The Events Calendar

Question

I am trying, programmatically, to alter the permalink structure of events created by The Events Calendar plugin. I already have other posts and pages set-up and don’t want to change the permalink settings.

So the plugin currently creates events with structure:

mysite.com/event/some_event_name

The current site (non-WordPress) currently uses:

mysite.com/events/event_year/some_event_name

I would like to keep that structure. I have code that creates a permalink, or at least partly:

function my_custom_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_permastruct('tribe_events', '/events/%year%/%name%/', false);
    $wp_rewrite->flush_rules();
}
add_action('init', 'my_custom_rewrite');

function my_custom_permalinks( $permalink, $post ) {
    if  ($post->post_type == 'tribe_events') {

        $meta = get_post_meta($post->ID);
        $newDate = date("Y", strtotime( $meta['_EventStartDate'][0] ));
        return str_replace( array('%year%','%name%'), array($newDate,$post->post_name), $permalink );
  }
  return $permalink;
}
add_filter( 'post_type_link', 'my_custom_permalinks', 10, 2 );

I know it is not perfect.

mysite.com/event/some_event_name now redirects to mysite.com/events/event_year/some_event_name. Perfect but mysite.com/events/event_year/some_event_name shows 404. I have tried a number of hooks, pre_post, etc… but can’t seem to get WordPress to show the event on the new URL. Can anyone point me to the correct hooks to use to finish this/get it working?

Thanks.

0
, nickbits 3 years 2019-10-31T05:51:56-05:00 0 Answers 82 views 0

Leave an answer

Browse
Browse