Custom permalinks for custom post type and taxonomy
-
I’m trying to create a custom permalink structure based on taxonomy terms. I’ve got it partially working, however I’m having a couple of issues that I can’t seem to figure out.
Here’s my functions for registering my post type and taxonomy and creating the permalink structure:
function create_services() { register_post_type('services', array( 'label' => 'Services','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => true,'rewrite' => array(slug=>'%locations%', with_front=>false),'query_var' => true,'exclude_from_search' => false,'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes',),'labels' => array ( 'name' => 'Services', 'singular_name' => 'Service', 'menu_name' => 'Services', 'add_new' => 'Add Service', 'add_new_item' => 'Add New Service', 'edit' => 'Edit', 'edit_item' => 'Edit Service', 'new_item' => 'New Service', 'view' => 'View Service', 'view_item' => 'View Service', 'search_items' => 'Search Services', 'not_found' => 'No Services Found', 'not_found_in_trash' => 'No Services Found in Trash', 'parent' => 'Parent Service', ),) ); } add_action('init', 'create_services'); function create_locations() { register_taxonomy('locations',array ( 0 => 'services',),array( 'hierarchical' => true, 'label' => 'Locations','show_ui' => true,'query_var' => true,'rewrite' => array(slug=>'', with_front=>true),'singular_label' => 'Location') ); } add_action('init', 'create_locations'); add_filter('post_link', 'location_permalink', 10, 3); add_filter('post_type_link', 'location_permalink', 10, 3); function location_permalink($permalink, $post_id, $leavename) { if (strpos($permalink, '%locations%') === FALSE) return $permalink; // Get post $post = get_post($post_id); if (!$post) return $permalink; // Get taxonomy terms $terms = wp_get_object_terms($post->ID, 'locations'); if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug; else $taxonomy_slug = 'no-location'; return str_replace('%locations%', $taxonomy_slug, $permalink); }
Problem #1: The permalink structure I’m trying to get for this post type is:
https://domain.com/%taxonomyterm%/%postname%/
— This seems to be working.However upon adding this to my functions, permalinks for pages and everything else stop working. They are using the permalink structure set in the WordPress settings of
/%postname%/
If I revert these settings back to the default, the pages and all show up fine, with exception of my custom post type permalink rewrite.
Problem #2: If I were to create a post in my custom post type with the name “Plumbing” and assign it to a particular taxonomy term “Big Bear, CA”, my permalink shows
https://domain.com/big-bear-ca/plumbing
. This is great, however, if I create another post with the same name “Plumbing” but assign it to a a different taxonomy term “Victorville, CA”, my permalink showshttps://domain.com/victorville-ca/plumbing-2
.How can I get the post name to not append the “-2”? Since it’s theoretically under a different url I would think that WordPress would be able to handle it..unless WordPress logic works differently.
Is there a way to get clean, pretty URLs for this setup?
TIA!
- The topic ‘Custom permalinks for custom post type and taxonomy’ is closed to new replies.