Title: Complicated Permalink Structure
Last modified: September 1, 2016

---

# Complicated Permalink Structure

 *  Resolved [neverStopAL](https://wordpress.org/support/users/neverstopal/)
 * (@neverstopal)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/complicated-permalink-structure/)
 * I have read alot of articles and done quiet a bit of filtering. I can accomplish
   parts of my requirement with any given filter or plugin, but when I combine them,
   I do not succeed. I need to:
    1.  Remove Parent level category from slug. This works fine, and also works with
        CPTs with a modification.
    2.      ```
            add_filter ( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
            function remove_parent_cats_from_link( $permalink, $post, $leavename ) {
        
            	$cats = get_the_category ( $post->ID );
            	if ( $cats ) {
            		usort ( $cats, '_usort_terms_by_ID' ); // order by ID
            		$category = $cats[ 0 ]->slug;
            		if ( $parent = $cats[ 0 ]->parent ) {
            			$parentcats = get_category_parents ( $parent, false, '/', true );
            			$permalink  = str_replace ( $parentcats, '', $permalink );
            		}
            	}
        
            	return $permalink;
            }
            ```
        
    3.  Add /%category%/ with removed parent to 3 custom post types. I added Custom
        Post Type Permalinks plugin and it works great with the /%category%/ for each
        CPT. It even picks up the filter to remove the parents with this
         `add_filter(‘
        post_type_link’, ‘remove_parent_cats_from_cpt’, 10, 3 ); function remove_parent_cats_from_cpt(
        $permalink, $post, $leavename ) {
    4.   if ( ( ‘hmk_recipe’ == get_post_type ( $post ) ) || ( ‘hmk_craft’ == get_post_type(
        $post ) ) || ( ‘hmk_article’ == get_post_type ( $post ) ) ) {
    5.   $cats = get_the_category ( $post->ID );
         if ( $cats ) { usort ( $cats, ‘_usort_terms_by_ID’);//
        order by ID $category = $cats[ 0 ]->slug; if ( $parent = $cats[ 0 ]->parent){
        $parentcats = get_category_parents ( $parent, false, ‘/’, true ); $permalink
        = str_replace ( $parentcats, ”, $permalink ); } }
    6.   }
    7.   return $permalink;
         }`
    8.  Remove the /custom-post-type/ base from those 3 types of posts too. When I 
        filter to remove that from the url, I get 400 errors. Here’s the code.
    9.      ```
            function hmk_remove_cpt_slug_craft( $post_link, $post, $leavename ) {
            	if (  'hmk_craft'  != $post->post_type   ||  'publish' != $post->post_status ) {
            		return $post_link;
            	}
        
            	$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
            //	$post_link = str_replace( '/' . $post->post_type . '/', '/%category%/', $post_link );
        
            	return $post_link;
            }
        
            add_filter( 'post_type_link', 'hmk_remove_cpt_slug_craft', 10, 3 );
        
            function hmk_parse_request( $query ) {
        
            	if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
            		return;
            	}
        
            	if ( ! empty( $query->query['name'] ) ) {
            		$query->set( 'post_type', array( 'post', 'hmk_article', 'hmk_craft', 'page' ) );
            	}
            }
            add_action( 'pre_get_posts', 'hmk_parse_request' );
            ```
        
 * I feel like I’m doing too much filtering on these permalinks, and because I have
   used the plugin to add the /%category%/, I wonder if that’s not causing a conflict
   with my filters.
 * Also for the CPTs I have tried turning off the plugin and just adding /%category%/
   to my filters. You may see that commented out. No luck.
 * Also I tried adding /%category%/ in the $rewrite options when I create the CPTs.
   Still no luck.
 * I am pretty green with this Rewrite API, so your help or advice is greatly appreciated.

Viewing 1 replies (of 1 total)

 *  Thread Starter [neverStopAL](https://wordpress.org/support/users/neverstopal/)
 * (@neverstopal)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/complicated-permalink-structure/#post-8184447)
 * We made the decision to remove the top level categories. All the other filters
   work ok now with Custom Post Types.

Viewing 1 replies (of 1 total)

The topic ‘Complicated Permalink Structure’ is closed to new replies.

## Tags

 * [permalinks](https://wordpress.org/support/topic-tag/permalinks/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [neverStopAL](https://wordpress.org/support/users/neverstopal/)
 * Last activity: [9 years, 6 months ago](https://wordpress.org/support/topic/complicated-permalink-structure/#post-8184447)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
