Better URL formatting with Custom Post Types and Taxonomies

I’m using Toolset Types and wondering how easy it is to set up the URL’s how I want.

I have a custom post type of venues and I have a custom category taxonomy of location.

Read More

Currently the urls are coming out like

http://domain.com/venue/location/manchester/
http://domain.com/venue/manchester/the-venue-name/

But I want the URL’s to be structured like

http://domain.com/manchester/
http://domain.com/manchester/the-venue-name/

Where do I need to look to make these changes?

Is this all .htaccess work or can something be done within the permalinks section?

Thanks in advance

Related posts

1 comment

  1. If i understand right, this hack must work in your template.
    First of all we have to remove the Post type name from url Slug.

    function ft_remove_postType_slug_fromUrl( $post_link, $post, $leavename ) {
        if ( 'venue' != $post->post_type || 'publish' != $post->post_status ) {
            return $post_link;
        }
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
        return $post_link;
    }
    add_filter( 'post_type_link', 'ft_remove_postType_slug_fromUrl', 10, 3 );
    

    But this is not gonna work by itself. If u pate this code in your functions.php u should get 404 Error.Bbecause WordPress only expects posts and pages to behave this way.

    So, you have to add this action also.

    function ft_change_parsingRequest( $query ) {
    
        if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
            return;
        }
        if ( ! empty( $query->query['name'] ) ) {
            $query->set( 'post_type', array( 'post', 'venue', 'page' ) );
        }
    }
    add_action( 'pre_get_posts', 'ft_change_parsingRequest' );
    

    After this, you renew / refresh yours post type / permalink tree (It calls flush_rewrites i guess.), It means, re-update your permalink settings on your admin panel area.

    Or if you want to see or do some magic, you can check it out from source url.

    https://core.trac.wordpress.org/browser/tags/4.3/src/wp-includes/post.php#L1454

    This Line says;

    add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
    

    Happy Coding.

Comments are closed.