Support Forum
Support › MB Custom Post Type › Post Type and then Taxonomy for URL StructureResolved
Hi,
I use Bricks Builder and Meta Box AIO.
I'm trying something that makes sense to do and I can't get it to work.
I have created a custom post type (lets call it "articles").
I have created a custom category taxonomy (lets call it "category").
I want the following URL structure where the taxonomy uses the CPT as part of the URL structure. This taxonomy is only being used for this CPT.
/articles/article-slug
/articles/category/category-slug
For the Custom Taxonomy I have set "Custom rewrite slug" to "articles/category"
I can get this to work in ACF and Jet Engine but not Metabox.
It looks like for some reason "Custom rewrite slug" cannot contain a CPT slug in it. If it does I either get a 404 page or if the slug for the Custom Taxonomy matches another CPT post the URL will be redirected to that post. If I change "Custom rewrite slug" to something else like "some-text/category" it works fine (but then the URL structure does not make sense).
How can I fix this so that the category the CPT is for follows it in the URL structure like
/articles/category/category-slug
Similar questions has been asked several times since 2021:
https://support.metabox.io/topic/post-type-and-taxonomy-url-structure/
https://support.metabox.io/topic/2-doubts-about-cpts-and-taxonomy-urls/
https://support.metabox.io/topic/custom-rewrite-slug-doesnt-work/
and nothing has been done about it. I am not sure why since this obviously something that makes sense to do for a Custom Taxonomy archive.
This article seems to indicate the fix is to make sure the custom taxonomy is registered before the CPT.
https://cnpagency.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/
Is this possible with the MetaBox user interface?
I just tried registering the taxonomy and CPT by code (with the taxonomy first and setting the ones I have in the MetaBox UI to draft status) using code and it didn't seem to make a difference
Okay I figured it out. Here is my solution:
// Example URLs (In WordPress settings, set Permalink Structure to 'plain')
// https://example.com/?ess_article_category=news
// https://example.com/?paged=2&ess_article_category=news
function ess_article_category_taxonomy_rewrites(){
// Setup in Metabox AIO
$cpt = 'ess_article';
$cpt_rewrite = 'articles';
$ctax = 'ess_article_category';
$ctax_rewrite = 'categories';
// For Taxonomy Archive with multiple pages
// This must go before rule for main taxonomy archive page below else it will never get triggered
add_rewrite_rule(
$cpt_rewrite . '\/' . $ctax_rewrite . '\/([^\/]+)\/page\/([0-9]+)',
'index.php?' . $ctax . '=$matches[1]&paged=$matches[2]',
'top'
);
// Match = CPT / Custom Taxonomy / Taxonomy slug variable
// Must escape delimiter / with \ so = \/
// ([^\/]+) finds and returns text in matches array
// ([0-9]+) finds and returns number in matches array
add_rewrite_rule(
$cpt_rewrite . '\/' . $ctax_rewrite . '\/([^\/]+)',
'index.php?' . $ctax . '=$matches[1]',
'top'
);
}
add_action( 'init', 'ess_article_category_taxonomy_rewrites' );
Thanks for sharing your solution 🙂