1

I'm working on a WP website with many CPTs and Custom Taxonomies, but also using the regular "post" post type for the regular blog section of the site.

I want the blog posts and archive and categories to have /blog/ in their permalink, but also want to specify permalink structure for the other CPTs without the /blog/ interfering.

for example: I have an "events" CPT, which has a custom taxonomy named "ecat".

Now I got an archive page with permalink sitename.com/events working well

And the single event page has permalink like sitename.com/events/event-name as well.

But the custom taxonomy archive is in sitename.com/ecat/ategory-name. I want the custom taxonomy to have the cpt slug in its permalink like: sitename.com/events/ecat/category-name

Note: I don't want the single event to have the taxonomy term in its permalink.

My CPT registration Code:

    $labels = array(
    'name'                  => _x( 'Events', 'Post Type General Name', 'textdomain' ),
    'singular_name'         => _x( 'Event', 'Post Type Singular Name', 'textdomain' ),
    'menu_name'             => __( 'Events', 'textdomain' ),
    'name_admin_bar'        => __( 'Event', 'textdomain' ),
    'archives'              => __( 'Item Archives', 'textdomain' ),
    'attributes'            => __( 'Item Attributes', 'textdomain' ),
    'parent_item_colon'     => __( 'Parent Item:', 'textdomain' ),
    'all_items'             => __( 'All Items', 'textdomain' ),
    'add_new_item'          => __( 'Add New Item', 'textdomain' ),
    'add_new'               => __( 'Add New', 'textdomain' ),
    'new_item'              => __( 'New Item', 'textdomain' ),
    'edit_item'             => __( 'Edit Item', 'textdomain' ),
    'update_item'           => __( 'Update Item', 'textdomain' ),
    'view_item'             => __( 'View Item', 'textdomain' ),
    'view_items'            => __( 'View Items', 'textdomain' ),
    'search_items'          => __( 'Search Item', 'textdomain' ),
    'not_found'             => __( 'Not found', 'textdomain' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'textdomain' ),
    'featured_image'        => __( 'Featured Image', 'textdomain' ),
    'set_featured_image'    => __( 'Set featured image', 'textdomain' ),
    'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
    'use_featured_image'    => __( 'Use as featured image', 'textdomain' ),
    'insert_into_item'      => __( 'Insert into item', 'textdomain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'textdomain' ),
    'items_list'            => __( 'Items list', 'textdomain' ),
    'items_list_navigation' => __( 'Items list navigation', 'textdomain' ),
    'filter_items_list'     => __( 'Filter items list', 'textdomain' ),
);
$args = array(
    'label'                 => __( 'Event', 'textdomain' ),
    'description'           => __( 'Events custom post type', 'textdomain' ),
    'labels'                => $labels,
    'supports'              => array( 'title' ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-calendar-alt',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'page',
    'show_in_rest'          => false,
    'rewrite' => array( 'slug' => 'events','with_front' => false ),
);
register_post_type( 'events', $args );

My Custom Taxonomy register code:

//Event Categories
$labels = array(
    'name'                       => _x( 'Events Categories', 'textdomain' ),
    'singular_name'              => _x( 'Events Category', 'textdomain' ),
    'menu_name'                  => __( 'Events Categories', 'textdomain' ),
    'all_items'                  => __( 'All Items', 'textdomain' ),
    'parent_item'                => __( 'Parent Item', 'textdomain' ),
    'parent_item_colon'          => __( 'Parent Item:', 'textdomain' ),
    'new_item_name'              => __( 'New Item Name', 'textdomain' ),
    'add_new_item'               => __( 'Add New Item', 'textdomain' ),
    'edit_item'                  => __( 'Edit Item', 'textdomain' ),
    'update_item'                => __( 'Update Item', 'textdomain' ),
    'view_item'                  => __( 'View Item', 'textdomain' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'textdomain' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'textdomain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'textdomain' ),
    'popular_items'              => __( 'Popular Items', 'textdomain' ),
    'search_items'               => __( 'Search Items', 'textdomain' ),
    'not_found'                  => __( 'Not Found', 'textdomain' ),
    'no_terms'                   => __( 'No items', 'textdomain' ),
    'items_list'                 => __( 'Items list', 'textdomain' ),
    'items_list_navigation'      => __( 'Items list navigation', 'textdomain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => false,
    'show_in_rest'               => false,
);
register_taxonomy( 'ecat', array( 'events' ), $args );
0

2 Answers 2

0

Welcome to WP StackExchange! You may want to try something like this, which uses the same rewrite setting as your register CPT:

$args = array(
    'rewrite'      => array('slug' => 'events/ecat', 'with_front' => false)
);
register_taxonomy( 'ecat', array( 'events' ), $args );

Sources:

https://cnpagency.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/

https://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments

1
  • When I add this line to my taxonomy registration code - the taxonomy archive gives 404 for the sitename.com/events/ecat/category-name (Tried to resave the permalinks settings)
    – glng
    Jul 8, 2019 at 12:39
0

Prefix for post type post

Here you will find how to add prefix only for post type "post".

In the same way, you can add a prefix for the category. Suitable filter hooks are: category_rewrite_rules, pre_term_link.

add_filter( 'pre_term_link', 'se342399_pre_term_link', 20, 2 );
add_filter( 'category_rewrite_rules', 'se342399_category_rewrite_rules', 20 );

function se342399_pre_term_link( $termlink, $term ) 
{
    // add prefix only to links for terms of taxonomy "category"
    if ( $term instanceof \WP_Term && $term->taxonomy == 'category' ) {
        $termlink = '/blog' . $termlink;
    }
    return $termlink;
}

function se342399_category_rewrite_rules( $category_rewrite ) 
{
    if ( is_array($category_rewrite) ) 
    {
        $new_rules = [];
        foreach( $category_rewrite as $k => $v) {
            $new_rules[ 'blog/' . $k ] = $v;
        }
        $category_rewrite = $new_rules;
    }
    return $category_rewrite;
}

CPT slug before taxonomy ecat

To add slug of custom post type as prefix in term links of ecat taxonomy you can use one of the following methods:

  • add rewrite to $args parameter of register_taxonomy() (as @Tom suggested)

    "rewrite" => array( 'slug' => 'events/ecat', 'with_front' => false, )
    
  • or follow as in the case of categories, using hooks ecat_rewrite_rules ({$permastructname}_rewrite_rules) and pre_term_link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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