I'm trying to create a custom permalink for a custom post type and have it only apply to a specific category. For example I want to change this URL from: http://www.example.com/events/event-name to http://www.example.com/webinars/event-name.
This is the code I have so far which doesn't work:
function change_webinar_links($permalink) {
global $post;
if(get_post_type() == 'tribe_events' && has_category('webinar', $post->ID)) {
$permalink = trailingslashit( home_url('/webinars/' . $post->post_name ) );
}
return $permalink;
}
add_action( 'init', 'webinar_rewrite_rule');
function webinar_rewrite_rule() {
global $post;
if ( has_category('webinar', $post->ID) ) {
add_rewrite_rule( '^webinars/([^/]+)/?', 'index.php?tribe_events=$matches[1]&post_type=tribe_events&name=$matches[1]', 'top' );
}
}