1

I need the following url structures for my Custom Posttype and Custom Taxonomy. Can anyone please help in achieving them? Spent a lot of time but failed. Ton of thanks in advance.

Here's my setup

add_action( 'init', 'custom_post_type_intervention' );
function custom_post_type_intervention(){
$labels = array(
        'name'          => 'Interventions',
        'singular_name' => 'Intervention',
        'add_new'       => 'Add Intervention',
        'add_new_item'  => 'New Intervention',
        'edit_item'     => 'Edit Intervention',
        'all_items'     => 'All Interventions',
        'menu_name'     => 'Interventions'
        );
    $args = array(
        'labels'    => $labels,
        'public'    => true,
        'publicly_queryable' => true,
        'show_ui'   => true,
        'show_in_menu' => true,
        'show_in_rest' => true,
        'query_var' => true,
        'taxonomies' => array( 'intervention_cat' ),
        'has_archive' => 'interventions',       
        'show_in_nav_menus' => true,
        'menu_position' => 6,
        'menu_icon' => 'dashicons-rest-api',
        'supports' => array( 'title', 'editor', 'author', 'revisions' ),
        'rewrite' => array( 'slug' => 'interventions/%intvn-cat%', 'with_front' => false )
        );
    register_post_type( 'intervention', $args );
    unset( $args, $labels );
    
    $labels = array(
        'name' => 'Categories',
        'singular_name' => 'Category'
        );
    $args = array(
        'labels'    => $labels,
        'hierarchical' => true,
        'show_in_nav_menus' => true,
        'show_in_rest' => true,
        'rewrite' => array( 'slug' => 'interventions', 'with_front' => false ),
        'default_term' => array( 'name' => 'Uncategorized', 'slug' => 'uncategorized' ) 
        );
    register_taxonomy( 'intervention_cat', 'intervention', $args );
    unset( $args, $labels );            
    
    function intervention_custom_post_slug( $post_link, $post ){
        if( 'intervention' === $post->post_type ){
            $terms = wp_get_object_terms( $post->ID, 'intervention_cat' );
            if( $terms ){
                return str_replace( '%intvn-cat%', $terms[0]->slug, $post_link );
            }else{
                return str_replace( '%intvn-cat%', 'uncategorized', $post_link );
            }
        }
        return $post_link;
    }
    add_rewrite_tag( '%intvn-cat%', '(.+)' );
    add_filter( 'post_type_link', 'intervention_custom_post_slug', 1, 3 );
    //add_rewrite_rule( '^interventions/(.*)/?$', 'index.php?intervention_cat=$matches[1]', 'top' );
}

2 Answers 2

0

There are 2 possible ways to fix the issue with the term's permalinks (which is currently using the wrong query var name – intvn-cat):

  1. Easy. Just set the 3rd parameter for add_rewrite_tag() to intervention_cat=. I.e. add_rewrite_tag( '%intvn-cat%', '(.+)', 'intervention_cat=' );

  2. This is what I would actually do – Remove the add_rewrite_tag() part in your code, then replace all of the %intvn-cat% instances with %intervention_cat%.

And then, be sure to flush the rewrite rules by simply visiting the permalink settings admin page.

Additionally, for performance reasons, functions like get_the_terms() (which the results of has been cached), should be used. See https://developer.wordpress.org/reference/functions/wp_get_object_terms/#more-information.

0

To achieve the desired Term URL structure (https://www.domain.com/interventions/taxonomy-slug/) for your custom taxonomy in WordPress, you'll need to adjust your rewrite rules and ensure that your taxonomy is correctly set up to handle these URLs. From the code you've provided, it looks like you're on the right track, but you need to uncomment and possibly adjust the rewrite rule for the taxonomy.

Here's what you need to do:

Uncomment the Rewrite Rule: Uncomment the add_rewrite_rule line in your code. This rule is necessary for WordPress to understand how to handle URLs that include your custom taxonomy.

Adjust the Rewrite Rule: Ensure that the rewrite rule correctly captures the taxonomy slug and maps it to the correct query variable. The current rule looks like it should work, but you might need to adjust it based on your exact requirements.

Flush Rewrite Rules: After making changes to rewrite rules, it's important to flush the rewrite rules to ensure that WordPress recognizes the new URL structure. You can do this by visiting the Settings > Permalinks page in your WordPress admin area and simply clicking "Save Changes".

Taxonomy Query Variable: Make sure that the query variable you're using (intervention_cat) matches the one registered with your taxonomy.

Here's the adjusted part of your code with the rewrite rule uncommented:

add_action( 'init', 'custom_post_type_intervention' );
function custom_post_type_intervention(){
    // ... [rest of your code]

    add_rewrite_tag( '%intvn-cat%', '(.+)' );
    add_filter( 'post_type_link', 'intervention_custom_post_slug', 1, 3 );
    add_rewrite_rule( '^interventions/([^/]+)/?$', 'index.php?intervention_cat=$matches[1]', 'top' );
}

In this rewrite rule, ^interventions/([^/]+)/?$ captures any URL that starts with interventions/ followed by any character sequence that is not a slash (/), then optionally ends (the /?$ part). The captured part is then passed to the intervention_cat query variable.

Remember, any time you change rewrite rules, visit the Permalinks settings page and save the changes to flush the rules. If the URLs still don't work as expected, you may need to debug your setup further, checking for conflicts with other plugins or themes, or issues in your code.

3
  • Thanks @wordpress-buddha, your suggested regex seems working. I've created a template as "taxonomy-intervention_cat.php" but not loading. I also tried to force it using "template_include" filter but surprisingly it loads "index.php". function custom_taxonomy_intervention_cat_template( $template ) { if( is_tax( 'intervention_cat' ) ) { $template = get_stylesheet_directory(). '/taxonomy-intervention_cat.php'; } return $template; } add_filter( 'template_include', 'custom_taxonomy_intervention_cat_template');
    – TechieBoy
    Commented Feb 1 at 4:40
  • @TechieBoy there is no need for the add_rewrite_rule() there, because WordPress will automatically generate a similar rewrite rule, so just remove that add_rewrite_rule() line. But the problem as stated in my answer, is that the query var is wrong because you didn't specify it when you call add_rewrite_tag(), hence WordPress defaults to using intvn-cat instead of intervention_cat which is the correct one based on your taxonomy args. So, try using the same code as in your question and apply the first option in my answer, flush the rules and then see if it worked.
    – Sally CJ
    Commented Feb 1 at 6:56
  • 2
    Thanks @sally-cj it worked!
    – TechieBoy
    Commented Feb 3 at 6:07

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.