0

I have created a custom post type with two custom taxonomies, which will eventually be used to filter the results in the custom post type. The problem I am having is that I would like the URL to read:

site-url/custom-post-type/custom-taxonomy

I've tried using some of the suggestions for people with similar problems on here, but I cannot get it working in this instance. This is the code I have at the moment, I've stripped it back as the attempts to get this working were in vain:

//Custom Post Types

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'listing',
    array(
        'labels' => array(
            'name' => __( 'Listings' ),
            'singular_name' => __( 'Listing' )
        ),
    'public' => true,
    'has_archive' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => array( 'slug' => 'directory', 'with_front' => false ),
    'query_var' => false,
    'supports' => array( 'title', 'editor' )
    )
);
}

//Region Taxonomies

function region_taxonomy() {  
register_taxonomy(  
'region',  
'listing',  
array(  
    'hierarchical' => true,  
    'label' => 'Regions',  
    'query_var' => true,  
    'rewrite' => array('slug' => 'region', 'with_front' => true )  
)  
);  
}  

add_action( 'init', 'region_taxonomy' );

//Qualifications Taxonomies
function qualification_taxonomy() {  
register_taxonomy(
'qualification',  
'listing',  
array(  
    'hierarchical' => true,  
    'label' => 'Qualifications',  
    'query_var' => true,  
    'rewrite' => array('slug' => 'qualification' )  
)  
);  
}

Currently this produces:

site-url/region/ or /qualification/

Would be great if someone can help me out with this, wasted hours going round in circles!

0

You can use WordPress' ability to accept custom rewrite rules to get this done. You can add the following to your functions file, or create a plugin (recommended).

function url_rewrites_wpanswers() {
    add_rewrite_rule('/custom-post-type-slug/([^/]*)?$','index.php?post_type=custom-post-type-name&custom-taxonomy-name=$matches[1]', 'top'); //lets you use /posttype/customtaxterm
}
add_action('init','url_rewrites_wpanswers');

You would have to add a line for each since otherwise yoursite.com/something1/something2 would end up pointing to:

yoursite.com/index.php?post_type=something1&custom-taxonomy-name=something2

Keep in mind, with the functionality that you are requesting there can be conflicts with your permalinks if they are set to /post-type/post-name

You can read and learn more here: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

  • Thanks for your quick response, but not entirely sure what to do with that? I've only just started getting this deep with Wordpress, so please treat me like an utter newb! – YumYumPandaBurgers Oct 18 '13 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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