Rewriting a custom-post-type permalink with taxonomy term before post-type?
-
Hi hope anyone can help me. Apologies if this is simple stuff, new to it all.
Ok heres my aim: Got a website that has two top level pages called commercial and domestic and in these pages there is other content that shares similar format. So for example i want case studies in both of commercial and domestic pages.
I thought it would be best to have case studies as a custom post-type (case_studies) and have have a taxonomy (work_type) that has the two terms: commercial and domestic to help distinguish what type of case study it is.
I thought to achieve this i would rewrite the cases-studies post types url to show the term first followed by the word case_studies. so commercial/Case_studies/single
I actually got the above to work, well almost! Got the rewritten page slug to function but when it did it broke all the top level pages and turned the commercial and domestic pages to categories. I think the reason those pages used categories pages was because the originals cant be found.
Did some research and i think everyone was saying that a custom field has to be prefixed by static text. Is there anyway of making it so i don’t need that text prefixed.
So current issue is this makes the case_study post type work %work_type%/case_studies but breaks top level pages. case_studies/%work_type% will work for everything, but it is not what i really want.
I have included the code below:
add_action( 'init', 'cptui_register_my_cpts' ); function cptui_register_my_cpts() { $labels = array( "name" => "Case Studies", "singular_name" => "Case Study", ); $args = array( "labels" => $labels, "description" => "", "public" => true, "show_ui" => true, "has_archive" => false, "show_in_menu" => true, "capability_type" => "post", "map_meta_cap" => true, "hierarchical" => true, "rewrite" => array( "slug" => "%work_type%/case_studies", "with_front" => false ), "query_var" => true, "supports" => array( "title", "thumbnail", "page-attributes" ), "taxonomies" => array( "work_type" ) ); register_post_type( "case_studies", $args ); // End of cptui_register_my_cpts() } add_action( 'init', 'cptui_register_my_taxes' ); function cptui_register_my_taxes() { $labels = array( "name" => "Work Type", "label" => "Work Type", ); $args = array( "labels" => $labels, "hierarchical" => true, "label" => "Work Type", "show_ui" => true, "query_var" => true, "rewrite" => array( 'slug' => '%work_type%', 'with_front' => false ), "show_admin_column" => true, ); register_taxonomy( "work_type", array( "testimonial", "case_studies" ), $args ); } add_filter('post_type_link', 'work_types_permalink_structure', 10, 4); function work_types_permalink_structure($post_link, $post, $leavename, $sample) { if (false !== strpos($post_link, '%work_type%')) { $work_types_type_term = get_the_terms($post->ID, 'work_type'); if (!empty($work_types_type_term)) $post_link = str_replace('%work_type%', array_pop($work_types_type_term)-> slug, $post_link); else $post_link = str_replace('%work_type%', 'uncategorized', $post_link); } return $post_link; }
- The topic ‘Rewriting a custom-post-type permalink with taxonomy term before post-type?’ is closed to new replies.