1

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' );
    }
}

1 Answer 1

0

I got an answer on the WordPress forums. Here is the working code for those looking to do something similar:

add_filter( 'post_type_link', 'change_webinar_links', 10, 2 ); function change_webinar_links( $link, $post) { if ( $post->post_type == 'tribe_events' && tribe_event_in_category('webinar') ) { $link = trailingslashit( home_url('/webinars/' . $post->post_name ) ); } return $link; } add_action( 'init', 'webinar_rewrite_rule', 5); function webinar_rewrite_rule() {
add_rewrite_rule( '^webinars/([^/]+)', 'index.php?tribe_events=$matches[1]&post_type=tribe_events&name=$matches[1]', 'top' ); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.