Permalinks Help?

Question

Just wondering if anyone can help, please. Basically I have a Custom Post Type ‘Courses’, with a
taxonomy of ‘Subjects’. When I create a Course Custom Post Type I name it something like Oxford English (this would be the English post for Oxford), I then assign it to the Subject of English. I’d like the URL for the custom post type post be:
domain.co.uk/courses/english/oxford

I have this working fine.

But then I’d also like a page:
domain.co.uk/courses/english – but when I set this page up, and click preview its obviously picking up the Taxonomy Category of English and trying to redirect to that – I have archive turned off, so instead its just taking me straight to:
domain.co.uk/courses/english/oxford instead of domain.co.uk/courses/english

Any idea why this might be happening? The only way for me to fix it, is to have my taxonomy as ‘course/%subject%’, instead of ‘courses/%subject%’.

Here is the code I currently have:

register_post_type( 'courses',
    array(
    'labels' => array(
      'name' => __( 'Courses' ),
      'singular_name' => __( 'Course' )
    ),
    'public' => true,
    'has_archive' => false,
    'rewrite' => array('slug' => 'course/%subject%'),
    )
);

register_taxonomy(
    'subject',  // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
    'courses',    // post type name
    array(
        'hierarchical' => true,
        'label' => 'Subjects', // display name
        'query_var' => true,
        'show_admin_column' => true,
        'show_ui'           => true,
        'query_var'         => true,
        'has_archive' => false,
        'show_in_rest' => true,
        'rewrite' => array(
            'slug' => 'subjects',    // This controls the base slug that will display before each term
            'with_front' => false  // Don't display the category base before
        )
    )
);

function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'courses' ){
    $terms = wp_get_object_terms( $post->ID, 'subject' );
    if( $terms ){
        return str_replace( '%subject%' , $terms[0]->slug , $post_link );
    }
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
0
, , , , M.T 3 months 0 Answers 62 views 0

Leave an answer