I added the code below and successfully changed my CPT slug for the taxonomy terms. My blog permalinks are /article/%postname% in case you wonder, and I’d like to keep it that way, so no clashes with CPTs.
It works fine and I got rid of the CPT slug in the single CPT’s permalink (“listings”).
function listing_type_link_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
if ( strpos('%listing_type%', $post_link) === 'FALSE' ) {
return $post_link;
}
$post = get_post($id);
if ( !is_object($post) || $post->post_type != 'listing' ) {
return $post_link;
}
$terms = wp_get_object_terms($post->ID, 'listing_type');
if ( !$terms ) {
return str_replace('/listing_type', '/', $post_link);
}
return str_replace('%listing_type%', $terms[0]->slug, $post_link);
}
add_filter('post_type_link', 'listing_type_link_filter_function', 1, 3);
It works fine, and instead of:
mysite.com/listing_types/tours/tour-name (CPT "listing" post)
those posts show as:
mysite.com/tours/tour-name
and they are found correctly for any of the following
mysite.com/listing_type/tours/tour-name
mysite.com/listings/tours/tour-name
mysite.com/listing_types/tours/tour-name
mysite.com/listing/tours/tour-name
Now I need to hook the same or similar filter to the term archive links filter, which I can’t find, if there’s any.
so instead of
mysite.com/cpt_slug/taxonomy_term
(archive page)
I’d get
mysite.com/taxonomy_term
(archive page)
â¦because the most important things in my site are those terms: listing_types, (tours, attractions, events) but I can’t let the “listings” or “listing_types” slugs to show, even when they ARE listings for me.
Do you know what hook and how should I filter it to replace the listing_type slug from the TERM ARCHIVE pages link?
Or is it easier to set the general permalinks settings page as %term%/%postname% and make the CPTs to use it, and customizing the regular posts instead? âsince most of defaults could benefit to the CPTs and the posts have no terms.
Thanks.
This worked fine for me, except the whole system above has some issue with my configuration (I made my wp installation Multisite) that broke all the other posts. They are all not-found, although the permalink is retrieved correctly every time, as it was found.
I’m opening a new question for that other issue here so you can also use the code above safely.