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.