URL rewrite and navigation structure for wordpress custom post type

In order to rewrite a custom post type’s URL, or permalink, you’ll need to filter the ‘post_type_link’, which is run whenever get_permalink() is called, allowing you to change a post’s permalink. I’ve include some guide code below.

It seems like you have a handle on the rewrite rules but let me know if you need more help.

add_filter( 'post_type_link', 'filter_the_post_type_link', 1, 4 );
function filter_the_post_type_link( $post_link, $post, $leavename, $sample ) {

    // Filter the permalink by post type
    switch( $post->post_type ) {

        case 'albums':

            // Build permalink
            $post_link = get_bloginfo( 'url' ) . '/artists/';

            // Get the artist
            $album_artist = ??

            // Add artist to permalink
            $post_link .= "{$album_artist}/";

            // Add album name to permalink
            $post_link .= "discography/{$post->post_name}";

            break;

        case 'songs':

            // Build permalink
            $post_link = get_bloginfo( 'url' ) . '/artists/';

            // Get the artist
            $album_artist = ??

            // Add artist to permalink
            $post_link .= "{$album_artist}/";

            // Get the album
            $album = ??

            // Add album to permalink
            $post_link .= "discography/{$album}/";

            // Add song name to permalink
            $post_link .= "{$post->post_name}";

            break;

    }

    return $post_link;

}

Leave a Comment