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 🙂
Hi Tom,
I am having a similar problem. So, based on this article, I have to recreate my posts, and before that, I have to register the Taxonomy first? So I have to start over the whole website right?
https://cnpagency.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/
I am using Oxygen and Meta Box by the way.
Thanks,
Peter
The article makes sense and I did try that solution out, but in the end it did not work for me. Since you cannot specify the order CPT and taxonomies are registered via the Metabox UI you have to use a PHP snippet (which Metabox will generate for you) and register it that way. I tried it and still had the same issues.
So to get it to work just leave your CPT and taxonomy setup in Metabox as is and then add the code snippet above to your website. Once you modify the variables to suit your needs:
` $cpt = 'ess_article';
$cpt_rewrite = 'articles';
$ctax = 'ess_article_category';
$ctax_rewrite = 'categories';
the rewrites should work fine.