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.

I have found this place to be a good source of information in the past through a lot of Googling for the problems I have run into. My question pertains to the verbose rewrite rules WordPress uses.

I have set up a custom post type called project, and I have registered a custom taxonomy called projects. Everything works great except for the rewrite slug options as they end up conflicting - most likely due to the rewrite rules.

Basically this is the structure I am looking to achieve:

  • example.com/work/%taxonomy%/%post_name%/ (for posts)
  • example.com/work/%taxonomy%/ (list posts belonging to a particular taxonomy term)
  • example.com/work/ (goes to page-work.php which includes taxonomy.php to list all posts associated with that taxonomy)

Here is the code I have so far, but I need help writing the WP_Rewrite rules as this is the bit I am a bit stumped on.

$labels = array(
    'name' => _x('Projects', 'post type general name'),
    'singular_name' => _x('Project', 'post type singular name'),
    'add_new' => _x('Add New', 'project item'),
    'add_new_item' => __('Add New Project'),
    'edit_item' => __('Edit Project'),
    'new_item' => __('New Project'),
    'view_item' => __('View Project'),
    'search_items' => __('Search Projects'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'hierarchical' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false),
    'show_ui' => true,
    '_builtin' => false, // It's a custom post type, not built in!
    'capability_type' => 'post',
    'query_var' => "project", // This goes to the WP_Query schema
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
);

register_post_type('project' , $args);

// Showcase Taxonomy
register_taxonomy('projects', array('project'), array(
    'public' => true,
    'hierarchical' => true,
    'label' => 'Project Categories', 
    'singular_label' => 'Project Category',
    'query_var' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false, 'hierarchical'=>true)
    )
);

Many thanks for your help! :-)

share|improve this question
1  
    
@ChristopherDavis Thanks, I'll look into those a bit more and see how I get on. –  matt_d_rat Jun 20 '11 at 0:19
1  
I think this question can be answered by looking at Mixing custom post type and taxonomy rewrite structures? If that question does not help you, please edit this question to indicate how it is different. –  Jan Fabry Jul 9 '11 at 13:18
add comment

3 Answers

Hope this can solve your problem

function my_custom_post_type() {
$labels = array(
    'name' => _x('Projects', 'post type general name'),
    'singular_name' => _x('Project', 'post type singular name'),
    'add_new' => _x('Add New', 'project item'),
    'add_new_item' => __('Add New Project'),
    'edit_item' => __('Edit Project'),
    'new_item' => __('New Project'),
    'view_item' => __('View Project'),
    'search_items' => __('Search Projects'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => 'Projects' 
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
        'hierarchical' => false,
        'has_archive' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false),
    'show_ui' => true,
    '_builtin' => false, // It's a custom post type, not built in!
    'capability_type' => 'post',
        'query_var' => true, // This goes to the WP_Query schema
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
);

register_post_type( 'work' , $args );

}
function my_custom_taxonomies() {

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

    register_taxonomy( 'taxonomy', array('work'), array (
                    'labels' => $labels,
                    'hierarchical' =>false,
                    'show_ui' => true,
                    'rewrite' => array( 'slug' => 'work/taxonomy'),
                    'query_var' => true,
                    'show_in_nav_menus' => true,
                    'public' => true,
            ));
}

add_action('init', 'my_custom_post_type', 0);
add_action('init', 'my_custom_taxonomies', 10);

what you need to create is archive-work.php (your post type archive) and taxonomy.php which will use to show your custom taxonomy archive.

share|improve this answer
    
don't forget to change the "taxonomy" for your own taxonomy name. Don't use the same value as your post_type. try to use category for the first try. work/category , register_taxonomy('category, array('work'), array( ...... –  nackle Dec 18 '11 at 17:07
add comment

I had the same problem and after a lot of struggling I ended up with this solution.
Just add this to your code

global $wp_rewrite;
$wp_rewrite->flush_rules(); 

function my_custom_post_type() {
    $labels = array(
        'name' => _x('Projects', 'post type general name'),
        'singular_name' => _x('Project', 'post type singular name'),
        'add_new' => _x('Add New', 'project item'),
        'add_new_item' => __('Add New Project'),
        'edit_item' => __('Edit Project'),
        'new_item' => __('New Project'),
        'view_item' => __('View Project'),
        'search_items' => __('Search Projects'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'Projects' 
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
            'hierarchical' => false,
            'has_archive' => true,
        'rewrite' => array('slug'=>'work', 'with_front'=>false),
        'show_ui' => true,
        '_builtin' => false, // It's a custom post type, not built in!
        'capability_type' => 'post',
            'query_var' => true, // This goes to the WP_Query schema
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
    );

    register_post_type( 'work' , $args );

    global $wp_rewrite;   
    $wp_rewrite->flush_rules();    // this should help 
}
share|improve this answer
5  
$wp_rewrite->flush_rules() should not be run that often, it should only be run on activation or deactivation hooks or as sparingly as possible. It says so here: codex.wordpress.org/Rewrite_API/flush_rules ALSO is pretty much the same function as this one: codex.wordpress.org/Function_Reference/flush_rewrite_rules –  Jared Jan 13 '12 at 22:09
    
On another note, this is how I accomplished it: pastebin.com/k7QvxKLi –  Jared Jan 13 '12 at 22:19
    
@Jared Thanks for pointing, but I couldn't figure out a way to accompolish this when this is integrated in our theme (ie not via plugin). Please suggest. –  Dipesh Kc Jan 17 '12 at 5:37
    
The code would go in functions.php in that case. The code for a plugin and a theme is exactly the same, the only difference is in themes it always goes in functions.php or a file included in functions.php –  Jared Jan 17 '12 at 9:05
2  
I would suggest using the after_switch_theme hook, it's new to 3.3 (IIRC). –  Cristian Feb 1 '12 at 17:18
add comment

A more detailed explanation is on another post, but here's the basic parts you need to add:

  1. Register your taxonomies and cpt's as you do. Make sure your rewrite slug for the taxo is "basename" and the rewrite slug for the cpt is "basename/%tax_name%".

  2. Tell wordpress what to do with "%tax_name%" like this:

    function filter_post_type_link($link, $post)
    {
    if ($post->post_type != 'custom_post_type_name')
        return $link;
    
    if ($cats = get_the_terms($post->ID, 'taxonomy_name'))
    {
        $link = str_replace('%taxonomy_name%',array_pop($cats)->term_id, link); // see custom function defined below
    }
    return $link;
    }
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);
    
share|improve this answer
add comment

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.