2

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

1
+50

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.

7
  • Hi there Thanks for the solution .. It does resolve the issue .. and i get permalink like '/resources/%resources%/resources-type/%resources_type%' but issue is there is also another permalink which is working i,e '/resources/%resources%/ .. I don't want that second permalink at all
    – terminator
    Nov 16, 2018 at 9:01
  • Ok, I'll update the answer later.
    – Sally CJ
    Nov 17, 2018 at 3:29
  • Hi Sally , thanks for helping out . Looking forward for your update
    – terminator
    Nov 18, 2018 at 8:41
  • Please check the updated answer and let me know how it goes.
    – Sally CJ
    Nov 20, 2018 at 5:20
  • How is it, @terminator? If you've already found a solution, do share it with us. But I appreciate it if you could upvote the answer since (I believe that) it did help you. :)
    – Sally CJ
    Nov 20, 2018 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.