why it not working
Because your post type’s permalink structure (which defaults to our-work/<post name/slug>
) does not contain the category.
So if you want the permalink structure for your post type (our-work
) be our-work/<category>/<post name/slug>
, here’s one way that works in WordPress 5.6 & 5.5, but uses just one of the post categories:
-
Add the
<category>
part to the post type’s rewrite slug — here we’re using%projects%
as the category placeholder, but you can use other tag like%project_cats%
:register_post_type( 'our-work', array( 'rewrite' => array( 'slug' => 'our-work/%projects%' ), // your other args ) );
-
Use the
post_type_link
hook to replace the above part (i.e.%projects%
) with the actual category slug:add_filter( 'post_type_link', 'my_post_type_link', 10, 2 ); function my_post_type_link( $post_link, $post ) { if ( $post && 'our-work' === $post->post_type ) { // Get all the "projects" terms assigned to the post. $terms = get_the_terms( $post, 'projects' ); // But use only the first term ($terms[0]) which is normally the most recent // term assigned to the post. So if you assigned it the terms Foo and later // Bar, $terms[0] would be the Bar term. $post_link = str_replace( '%projects%', $terms[0]->slug, $post_link ); } return $post_link; }
That’s all, and be sure to flush the rewrite rules (just visit the permalink settings page).