I have a custom post type named Manufacturer(manufacturer)
created using JetEngine
. I have also created a taxonomy named Manufacturer Categories(manufacturer-categories)
. Now what I want is when I have a post named Michelin
, under the Truck
taxonomy, I want it to look like mysite/manufacturers/truck/michelin
where manufacturers
is an additional string I add in the url. I have achieved this by using the code below using post_type_link
:
function add_my_rewrites() {
add_rewrite_tag( 'manufacturers', '([^/]+)' );
}
function manufacturer_post_type_link($link, $post) {
if ( 'manufacturers' == get_post_type( $post ) ) {
$manufacturer = get_the_terms($post->ID, 'manufacturer-categories');
$manufacturerslug = $manufacturer[0]->slug;
$link = str_replace('manufacturers', 'manufacturers/'.$manufacturerslug, $link);
}
return $link;
}
add_filter('post_type_link', 'manufacturer_post_type_link', 1, 3);
however, when I go to the post using the new permalink, I get 404, but when I visit the old permalink, the post is there. I tried adding the rewrite_tag but it did not help. Where and what did I miss? Really appreciate the help.
I have a custom post type named Manufacturer(manufacturer)
created using JetEngine
. I have also created a taxonomy named Manufacturer Categories(manufacturer-categories)
. Now what I want is when I have a post named Michelin
, under the Truck
taxonomy, I want it to look like mysite.com/manufacturers/truck/michelin
where manufacturers
is an additional string I add in the url. I have achieved this by using the code below using post_type_link
:
function add_my_rewrites() {
add_rewrite_tag( 'manufacturers', '([^/]+)' );
}
function manufacturer_post_type_link($link, $post) {
if ( 'manufacturers' == get_post_type( $post ) ) {
$manufacturer = get_the_terms($post->ID, 'manufacturer-categories');
$manufacturerslug = $manufacturer[0]->slug;
$link = str_replace('manufacturers', 'manufacturers/'.$manufacturerslug, $link);
}
return $link;
}
add_filter('post_type_link', 'manufacturer_post_type_link', 1, 3);
however, when I go to the post using the new permalink, I get 404, but when I visit the old permalink, the post is there. I tried adding the rewrite_tag but it did not help. Where and what did I miss? Really appreciate the help.
*** I'm not familiar with JetEngine, but this is what to do with core WordPress; maybe it will help. ***
The post_type_link
filter only tells WordPress to change the URL in links, it doesn't inform WordPress on how the page is requested:
Filters the permalink for a post of a custom post type.
To have the CPT permalink include the taxonomy term, set the rewrite
parameter of the CPT to array( 'slug' => 'manufacturers/%manufacturer-categories%' )
, and then adjust the callback for the post_type_link
filter to replace %manufacturer-categories%
with the term's slug.
So, with help from @caleb, this is what worked for me:
I had to add this in my functions.php file, since using JetEngine, I can't add an array argument to the rewrite
option of the custom post type.
add_filter( 'register_post_type_args', 'manufacturers_register_post_type_args', 10, 2 );
function manufacturers_register_post_type_args( $args, $post_type ) {
if ( 'manufacturers' === $post_type ) {
$my_args = array(
'rewrite' => array( 'slug' => 'manufacturers/%manufacturer-categories%', 'with_front' => false )
);
return array_merge( $args, $my_args );
}
return $args;
}
and my post_type_link
filter callback now looks like this,
function manufacturer_post_type_link($link, $post) {
if ( 'manufacturers' == get_post_type( $post ) ) {
$manufacturer = get_the_terms($post->ID, 'manufacturer-categories');
$manufacturerslug = $manufacturer[0]->slug;
$link = str_replace('manufacturers/%manufacturer-categories%', 'manufacturers/'.$manufacturerslug, $link);
}
return $link;
}
add_filter('post_type_link', 'manufacturer_post_type_link', 1, 3);
and now it worked. My final url for each Manufacturer post type looks like
mysite.com/manufacturers/heil-truck/durapack-pyhton-2
where heil-truck
is the term of manufacturer-categories
and durapack-python-2
is the post name/title.
Thanks so much @caleb for the help.