1

I have created a custom post type for events. Now within events post type I have created 2 categories. One is 'event' and another one is 'workshop'.

The 'event' category post url looks like

http://www.test.com/news-events/events/event-1/

and the 'workshop' category url looks like same as 'event'

http://www.test.com/news-events/events/test-5555/

Now how can I change the workshop category url to

http://www.test.com/news-events/workshops/test-5555/

The custom post type code is

function create_event_post_type() { // this is our custom function
    register_post_type( 'bp_events', // this is the post type we're going to be referring to in our code etc. bespoke name.
        array( // set some global parameters for the admin section, mainly text and buttons etc 
            'labels' => array(
                'name' => __( 'Events' ),
                'singular_name' => __( 'Event' ),
                'add_new' => __( 'Add Event' ),
                'add_new_item' => __( 'Add Event' ),
                'edit' => __( 'Edit Event' ),
                'edit_item' => __( 'Edit Event' ),
                'new_item' => __( 'New Event' ),
                'view' => __( 'View Event' ),
                'view_item' => __( 'View Event' ),
                'search_items' => __( 'Search Events' ),
                'not_found' => __( 'No Events found' ),
                'not_found_in_trash' => __( 'No Events found in Trash' ),
                'parent' => __( 'Parent Event' ),
                'description' => __( 'A custom post type that allows admin to add a repository of Events to the website.' ) // not sure where this appears but it's here for good measure!
            ),
            'public' => true, // visible to the public
            'menu_icon' => bp_events_DIR_URL . 'bp_events.png', // set the icon to appear next to the option in the admin menu
            'supports' => array( 'title', 'editor', 'page-attributes', 'revisions', 'thumbnail'), // activate these elements on the custom page. could also include 'excerpt' etc. use 'editor' to include the main editor window, 'revisions' etc
            'rewrite' => array( 'slug' => 'news-events/events', 'with_front' => true ), // remove any heirarchical slug in the url that precedes ours and replace with this
            'can_export' => true, // we will allow these event pages to be included in a wordpress export in the admin section
            'menu_position' => 6,
            'hierachical' => true
        )
    );
}

add_action( 'init', 'create_event_post_type' ); 

And the taxonomy code is

function bp_events_tax(){ 

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

    // now create the taxonomy itself
    register_taxonomy(
        'event-category',   // give the taxonomy a slug/name
        'bp_events',        // assocoate the taxonomy with a cpt
        array(
            'label' => __('Categories'),
            'labels' => $labels,
            'sort' => true,
            'hierarchical' => true,
            'args' => array('orderby' => 'term_order'),
            'rewrite' => array('slug' => 'events')
        )
    );
    // repeat both blocks for next taxonomy     

}
add_action( 'init', 'bp_events_tax' );

Any help is highly appreciated. Thanks in advance.

  • i don't think you can use taxonomies for permalinks without getting into some complex rewrites... why not just create another CPT for workshops and set it's rewrite slug to /news-events/workshops ? you can still have the event taxonomy and just add it to both post types. – majick Jun 9 '16 at 14:15
  • Yes I have thought about that option. But I was wondering if this can be achieved through one CPT. Thanks. – Prithviraj Mitra Jun 9 '16 at 14:22
  • you could try making the rewrite slug just /news-events and since it is already hierarchical make a post events and another workshops then assign the posts to have a parent of either... but I'm pretty sure I've tried this and it doesn't work for some reason - you probably need a rewrite as well like I said... but it is halfways there. – majick Jun 9 '16 at 14:27
  • @majick I think it's better to create another CPT. Also I don't want it to show in the menu bar. So I would like to use 'show_in_nav_menus' => false and keep the rewrite slug as /news-events/workshops. Next add event tax to both cpt. Let me try in that way. – Prithviraj Mitra Jun 9 '16 at 15:23

Your Answer

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

Browse other questions tagged or ask your own question.