0

Currently scratching my head as to how to get this to work.

I have registered a custom post type "guide", and have a custom taxonomy for the post type of "venue-type".

I am trying to get the URL slut to be

domain.com/guide/venue-type/{taxonomy}/

I've tried all sorts to get it to work, trying to re-write the slug, update the permalinks, and then get a 404 page each time.

add_action( 'init', 'register_guide_posttype' );
function register_guide_posttype() {
    $args = [
        'label'  => esc_html__( 'Guide', 'text-domain' ),
        'labels' => [
            'menu_name'          => esc_html__( 'Guide', 'mytheme' ),
            'name_admin_bar'     => esc_html__( 'Guide', 'mytheme' ),
            'add_new'            => esc_html__( 'Add Guide', 'mytheme' ),
            'add_new_item'       => esc_html__( 'Add new Guide', 'mytheme' ),
            'new_item'           => esc_html__( 'New Guide', 'mytheme' ),
            'edit_item'          => esc_html__( 'Edit Guide', 'mytheme' ),
            'view_item'          => esc_html__( 'View Guide', 'mytheme' ),
            'update_item'        => esc_html__( 'View Guide', 'mytheme' ),
            'all_items'          => esc_html__( 'All Guide', 'mytheme' ),
            'search_items'       => esc_html__( 'Search Guide', 'mytheme' ),
            'parent_item_colon'  => esc_html__( 'Parent Guide', 'mytheme' ),
            'not_found'          => esc_html__( 'No Guide found', 'mytheme' ),
            'not_found_in_trash' => esc_html__( 'No Guide found in Trash', 'mytheme' ),
            'name'               => esc_html__( 'Guide', 'mytheme' ),
            'singular_name'      => esc_html__( 'Guide', 'mytheme' ),
        ],
        'public'              => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'show_in_rest'        => true,
        'capability_type'     => 'post',
        'hierarchical'        => false,
        'has_archive'         => 'guide',
        'query_var'           => true,
        'can_export'          => true,
        'rewrite_no_front'    => false,
        'show_in_menu'        => true,
        'menu_position'       => 20,
        'menu_icon'           => 'dashicons-location',
        'supports' => [
            'title',
            //'editor',
            'thumbnail',
            'custom-fields',
            'page-attributes',
        ],
        'rewrite' => array('slug' => 'guide', 'with_front' => false),
    ];

    register_post_type( 'guide', $args );

And then the taxonomy

add_action( 'init', 'register_guide_tax_taxonomy' );
function register_guide_tax_taxonomy() {
    $args = [
        'label'  => esc_html__( 'Venue Type', 'mytheme' ),
        'labels' => [
            'menu_name'                  => esc_html__( 'Venue Types', 'mytheme' ),
            'all_items'                  => esc_html__( 'All Venue Types', 'mytheme' ),
            'edit_item'                  => esc_html__( 'Edit Category', 'mytheme' ),
            'view_item'                  => esc_html__( 'View Category', 'mytheme' ),
            'update_item'                => esc_html__( 'Update Category', 'mytheme' ),
            'add_new_item'               => esc_html__( 'Add new Category', 'mytheme' ),
            'new_item'                   => esc_html__( 'New Category', 'mytheme' ),
            'parent_item'                => esc_html__( 'Parent Category', 'mytheme' ),
            'parent_item_colon'          => esc_html__( 'Parent Category', 'mytheme' ),
            'search_items'               => esc_html__( 'Search Venue Types', 'mytheme' ),
            'popular_items'              => esc_html__( 'Popular Venue Types', 'mytheme' ),
            'separate_items_with_commas' => esc_html__( 'Separate Venue Types with commas', 'mytheme' ),
            'add_or_remove_items'        => esc_html__( 'Add or remove Venue Types', 'mytheme' ),
            'choose_from_most_used'      => esc_html__( 'Choose most used Venue Types', 'mytheme' ),
            'not_found'                  => esc_html__( 'No Venue Types found', 'mytheme' ),
            'name'                       => esc_html__( 'Venue Types', 'mytheme' ),
            'singular_name'              => esc_html__( 'Venue Type', 'mytheme' ),
        ],
        'public'               => true,
        'show_ui'              => true,
        'show_in_menu'         => true,
        'show_in_nav_menus'    => true,
        'show_in_quick_edit'   => true,
        'show_admin_column'    => true,
        'show_in_rest'         => true,
        'has_archive'          => true,
        'hierarchical'         => false,
        'query_var'            => true,
        'sort'                 => true,
        'rewrite' => true,
    ];
    register_taxonomy( 'venue-type', [ 'guide' ], $args );

1 Answer 1

0

You can do that by writing a function using the 'generate_rewrite_rules' Wordpress hook. https://developer.wordpress.org/reference/hooks/generate_rewrite_rules/

<?php

function guides_cpt_generating_rule($wp_rewrite) {
    $rules = array();
    $terms = get_terms( array(
        'taxonomy' => 'venue-type',
        'hide_empty' => false,
    ) );
   
    $post_type = 'guide';

    foreach ($terms as $term) {    
                
        $rules['guide/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&guide_post_type=$matches[1]&name=$matches[1]';
                        
    }

    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'guides_cpt_generating_rule');
1
  • I've just tried the above with the 'rewrite' => array('slug' => 'guide/venue-type', 'with_front' => false) set on the taxonomy, but I'm getting a 404 after saving the permalinks again Mar 21, 2023 at 23:11

Your Answer

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

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