I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.

The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.

What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.

Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.

I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.

I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.

The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything. enter image description here

This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.

Please help with suggestion on the best way I can achieve the custom taxonomy url.

Thank you all.


Try to rewrite the taxonomy url in the .htaccess

RewriteRule ^produce_category(.*)$ /$1 [L]

(Not tested)


At the beginning I will indicate that the expected structure conflicts with the default for custom post type produce posts.

The solution to the problem requires changing the links created for the custom categories and appropriate parsing of new links to display the right posts.

Use the pre_term_link filter to change links. One of the parameters passed is the WP_Term object for which the link is generated. If its taxonomy is produce_category then the new {cpt}/%{taxonomy}% structure should be returned, that is produce/%produce_category%.

add_filter( 'pre_term_link', 'se355448_pre_term_link', 10, 2 );

function se355448_pre_term_link( $termlink, $term )
    if ( ! $term instanceof \WP_Term || $term->taxonomy != 'produce_category' )
        return $termlink;

    return '/produce/%produce_category%';

Then, for links to be recognized correctly, use the add_rewrite_rule() function to add the necessary rewrite rules. As already mentioned, your target structure collides with links to custom post type produce posts, that's why you need rewrite rules for every term in produce_category taxonomy, and they should be before CPT produce rules.

add_action( 'init', 'se355448_rewrite_rules' );

function se355448_rewrite_rules()
    $cpt = 'produce';
    $tax = 'produce_category';
    //$terms = ['fruits-and-vegetables', 'fish-and-seafood'];
    $terms = get_terms([
        'taxonomy'  => $tax,
        'hide_empty' => false,
        'fields'    => 'id=>slug',
    if ( is_array($terms) && count($terms) )
        foreach( $terms as $slug )

Each term needs a rewrite rule, so after adding a new one, deleting it, or changing slug of existing one, rewrite rules need to be refreshed. This can be done by clicking "Save" in "Settings -> Permalinks".

The code below will automatically, using the created_{$taxonomy} and delete_{$taxonomy} action hooks, update rewrite rules after adding or removing terms from produce_category taxonomy.

add_action( 'created_produce_category', 'se355448_refresh_rules', 20);
add_action( 'delete_produce_category',  'se355448_refresh_rules' );

function se355448_refresh_rules()

Your Answer

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

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