How to prefix custom post type URL with custom post taxonomy term?

admin2025-01-07  4

For one of my CPT, when default permalink looks like
example/CPT_slug/%postname%

instead I would like a permalink like this :
example/%custom_taxonomy_term%/CPT_slug/%postname%

With regular categories and posts, it's easy to do, I just have to use %category%/%postname% for permalinks.

But I can't for the life of me achieve it for CPT and custom taxonomies. What am I doing wrong ?

My CPT slug is "activity", with posts like : "Food tour", "Dinner cruise", ... My Custom Taxonomy slug is "agence", with terms like "New York", "Miami", ...

So I'd like my URLs to be like

  • (different post from the first)

(where, ideally, in case of "same slug" situations, disambiguation would be dealt with)

I've tried this approach :

"rewrite" => array( "slug" => "%agence%/activity", "with_front" => false ),

in register_post_type

and

function add_agence_prefix_in_activity_url( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if ( is_object( $post ) && get_post_type($post) == 'activity' )
    {
        $terms = wp_get_object_terms( $post->ID, 'agence' );
        if ( $terms ) {
            return str_replace( '%agence%', $terms[0]->slug, $post_link );
        }
    }

    return $post_link;
}
add_filter( 'post_type_link', 'add_agence_prefix_in_activity_url', 1, 3 );

It works : it lets me display my "activity" post content.
But I get 404 errors when trying to display posts & pages with their usual /%postname% permalinks; and I don't know what to do to fix this.

So instead I tried using the plugin "Custom Post Type Permalinks" :
it almost works : pages & posts are not broken anymore, and it gives me URLs like
/**new-york**/food-tour
... whereas I would like
/**new-york**/activity/food-tour

So i'm almost there, but not quite. How should I proceed ? Is what I am trying to do even possible ?

For one of my CPT, when default permalink looks like
example.com/CPT_slug/%postname%

instead I would like a permalink like this :
example.com/%custom_taxonomy_term%/CPT_slug/%postname%

With regular categories and posts, it's easy to do, I just have to use %category%/%postname% for permalinks.

But I can't for the life of me achieve it for CPT and custom taxonomies. What am I doing wrong ?

My CPT slug is "activity", with posts like : "Food tour", "Dinner cruise", ... My Custom Taxonomy slug is "agence", with terms like "New York", "Miami", ...

So I'd like my URLs to be like

  • https://www.example.com/new-york/activity/food-tour
  • https://www.example.com/new-york/activity/dinner-cruise
  • https://www.example.com/miami/activity/food-tour (different post from the first)

(where, ideally, in case of "same slug" situations, disambiguation would be dealt with)

I've tried this approach :

"rewrite" => array( "slug" => "%agence%/activity", "with_front" => false ),

in register_post_type

and

function add_agence_prefix_in_activity_url( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if ( is_object( $post ) && get_post_type($post) == 'activity' )
    {
        $terms = wp_get_object_terms( $post->ID, 'agence' );
        if ( $terms ) {
            return str_replace( '%agence%', $terms[0]->slug, $post_link );
        }
    }

    return $post_link;
}
add_filter( 'post_type_link', 'add_agence_prefix_in_activity_url', 1, 3 );

It works : it lets me display my "activity" post content.
But I get 404 errors when trying to display posts & pages with their usual /%postname% permalinks; and I don't know what to do to fix this.

So instead I tried using the plugin "Custom Post Type Permalinks" :
it almost works : pages & posts are not broken anymore, and it gives me URLs like
https://www.example.com/activity/**new-york**/food-tour
... whereas I would like
https://www.example.com/**new-york**/activity/food-tour

So i'm almost there, but not quite. How should I proceed ? Is what I am trying to do even possible ?

Share Improve this question edited Oct 28, 2020 at 0:24 Baptiste asked Oct 28, 2020 at 0:19 BaptisteBaptiste 34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Ok, found the (a?) solution.

"rewrite" => array( "slug" => "activity", "with_front" => false ),

and

function my_injecter_agence_in_activite_url( $post_link, $id = 0 ) {
    $post = get_post( $id );

    if (  $post->post_type == 'activity'  ||  'publish' == $post->post_status  )
    {
        $terms = wp_get_object_terms( $post->ID, 'agence' );
        if ( $terms )
        {
            $post_link = str_replace("activity", $terms[0]->slug.'/activite', $post_link);
            return $post_link;
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'my_injecter_agence_in_activite_url', 1, 3 );


function my_rewrite_rules()
{
    add_rewrite_rule(
        '(.*)\/activity\/(.*)?$',
        'index.php?post_type=activity&name=$matches[2]',
        'top'
    );
}
add_action( 'init', 'my_rewrite_rules' );

I was pretty close. I think there was a problem with my initial str_replace.

Now, neither posts nor pages or activities are broken, everything seems ok.

The custom taxonomy term used as prefix in the URL is purely cosmetic and I don't have disambiguation if two activities have the same slug, but I think it's not supposed to happen as WP won't let me have two posts with two identic slugs.

Now I just have to find a way to make sure I get a 404 error if looking for an activity in the wrong term (for example, if trying to reach example.com/new-york/activity/bike-tour-in-miami)

If someone has a better way of doing this, please let me know.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259956a628.html

最新回复(0)