Custom permalink – append taxonomy name and term name at the end of permalink

I have

custom post type -> resources
custom taxonomy  -> resources_type

I want custom permalinks where category base will be appended at the end instead of front

For example: I add a new resource with title “First resource” and assign term as type1

What I get – http://example.com/resources/resources-type/type1/first-resource

What i want – http://example.com/resources/first-resource/resources-type/type1

I am using the following argument code in register post type :

'rewrite' => array("slug" => "/resources/resources-type/%resources_type%"),

And then using following code to replace the “%resources_type%” in above code

function wpa_course_post_link( $post_link, $id = 0 ){
  $post = get_post($id);  
  if ( is_object( $post ) ){
      $terms = wp_get_object_terms( $post->ID, 'resources_type' );
      if( $terms ){
        return str_replace( '%resources_type%' , $terms[0]->slug , $post_link );
      }
  }
  return $post_link;  
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );

1 Answer
1

You can achieve the following permalink structure by simply overriding (without having to remove) the default one (you specified) for the resources post type:

/resources/%resources%/resources-type/%resources_type% <- preferred
/resources/resources-type/%resources_type%/%resources% <- default

And you can override it via add_permastruct() — first, register the post type without specifying the rewrite parameter which means the rewrite defaults to true/enabled and that the slug would be resources (i.e. the post type key or slug):

register_post_type( 'resources', array(
    'label' => 'Resources',
    'public' => true,
    // no need to set the 'rewrite' arg
    ...
) );

Then right after that, override the permalink structure like so:

add_permastruct( 'resources', '/resources/%resources%/resources-type/%resources_type%' );

where the format is — the important part is the {POST TYPE KEY}, which in your case is resources:

add_permastruct( '{POST TYPE KEY}', '{PERMALINK STRUCTURE}' );

(Don’t forget to flush the rewrite rules — just visit the permalink settings page.)

How to disable /resources/%resources%

/resources/%resources% (i.e. /{POST TYPE KEY}/%{POST TYPE KEY}%) is automatically added if %{POST TYPE KEY}% is found in the permalink structure. You can remove it like so:

add_filter( 'resources_rewrite_rules', 'filter_resources_rewrite_rules' );
function filter_resources_rewrite_rules( $rules ) {
    $pattern = 'resources/([^/]+)/resources-type/';
    $p_length = strlen( $pattern );

    foreach ( $rules as $regex => $query ) {
        if ( $pattern !== substr( $regex, 0, $p_length ) ) {
            unset( $rules[ $regex ] );
        }
    }

    return $rules;
}

If the post type is hierarchical, then the $pattern would be resources/(.+?)/resources-type/.

And if you’d like to use the above code/function with another post type, you basically only need to replace the text “resources” with the proper post type key.

See https://developer.wordpress.org/reference/hooks/permastructname_rewrite_rules/ for more details on that filter.

Leave a Comment