Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is my first question so bear with me. I'm trying to setup a custom post type which has a few taxonomies associated with it. Each taxonomy is hierarchical. What I would like to achieve is a better url structure than what is happening right now. My permalinks are set to the default "post name" setting. What I would like to achieve is something like this: domain.com/custom-post-type/taxonomy/type

what ends up happening is I just get: domain.com/taxonomy/type

This is essentially a career board where I have different taxonomies representing different categories. For example there is "job sector" for different types of jobs. Should these be set as hierarchical?

When I use a plugin called "Custom Post Type Permalinks" I can get it to work the way I want but I was wondering if its possible without a plugin.

here is my code for the custom post type plugin I've created:

<?php
add_action( 'init', 'career_forum' );
// let's create the function for the custom type
function career_forum() { 
    $labels = array(
        'name' => _x( 'Careers', 'post type general name' ),
        'singular_name' => _x( 'Career Listing', 'post type singular name' ),
        'add_new' => __( 'Add New', 'career' ),
        'add_new_item' => __( 'Add New Career Listing' ),
        'edit_item' => __( 'Edit Career Listing' ),
        'new_item' => __( 'New Career Listing' ),
        'view_item' => __( 'View Career Listing' ),
        'search_items' => __( 'Search Careers' ),
        'not_found' => __( 'No careers found' ),
        'not_found_in_trash' => __( 'No careers found in trash' ),
        'parent_item_colon' => ''
    );
    $args = array( 
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true,
        'menu_position' => 5,
        'rewrite' => array( 'slug' => 'careers', 'with_front' => false ),
        'supports' => array( 'title', 'editor', 'thumbnail')
);

register_post_type( 'career_listings', $args );
}
add_action( 'init', 'create_career_forum_taxonomies', 0 );
// function for adding taxonomies for Career Forum
function create_career_forum_taxonomies() {
    // Job Sectors
    $labels = array(
        'name' => _x( 'Job Sectors', 'taxonomy general name' ),
        'singular_name' => _x( 'Job Sector', 'taxonomy singular name' ),
        'search_items' => __( 'Search Job Sectors' ),
        'all_items' => __( 'All Job Sectors' ),
        'parent_item' => __( 'parent Job Sector' ),
        'parent_item_colon' => __( 'Parent Job Sector:' ),
        'edit_item' => __( 'Edit Job Sector' ),
        'update_item' => __( 'Update Job Sector' ),
        'add_new_item' => __( 'Add New Job Sector' ),
        'new_item_name' => __( 'New Job Sector Name' ),
    );

register_taxonomy( 'job_sector', 
    array( 'career_listings' ), 
    array( 
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    //'rewrite' => array( 'slug' => 'job-sector', 'hierarchical' => true, 'with_front' => true ),
    )
);

// Regions
$labels = array(
    'name' => _x( 'Regions', 'taxonomy general name' ),
    'singular_name' => _x( 'Region', 'taxonomy singular name' ),
    'search_items' => __( 'Search Regions' ),
    'all_items' => __( 'All Regions' ),
    'parent_item' => __( 'parent Region' ),
    'parent_item_colon' => __( 'Parent Region:' ),
    'edit_item' => __( 'Edit Region' ),
    'update_item' => __( 'Update Region' ),
    'add_new_item' => __( 'Add New Region' ),
    'new_item_name' => __( 'New Region Name' ),
);

register_taxonomy( 'region', 
    array( 'career_listings' ), 
    array( 
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'region', 'hierarchical' => true ),
    )
);

// Position Type
$labels = array(
    'name' => _x( 'Position Types', 'taxonomy general name' ),
    'singular_name' => _x( 'Position Type', 'taxonomy singular name' ),
    'search_items' => __( 'Search Position Types' ),
    'all_items' => __( 'All Position Types' ),
    'parent_item' => __( 'parent Position Type' ),
    'parent_item_colon' => __( 'Parent Position Type:' ),
    'edit_item' => __( 'Edit Position Type' ),
    'update_item' => __( 'Update Position Type' ),
    'add_new_item' => __( 'Add New Position Type' ),
    'new_item_name' => __( 'New Position Type Name' ),
);

register_taxonomy( 'position_type', 
    array( 'career_listings' ), 
    array( 
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'position-type', 'hierarchical' => true ),
    )
);
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Hmm, couldn't you just fiddle around with the rewrite slug?

So, instead of 'slug' => 'position-type', you can use some other url: 'slug' => '/careers/position-type'.

In doing so, you may need to disable the rewrite for the custom post type.

Also don't forget to flush the rewrite rules for your changes to take effect, that took me a while to figure out. Basically it can be done by viewing the permalinks page in the admin site.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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