Support » Plugins » Hacks » post_type_link for custom permalink struct of custom types breaks editable slug

  • I have a simple post_type_link_filter which in conjunction with a rewrite rule lets me have a nice permalink structure for my custom post types (place them under the custom taxonomies use for them). However, the result is that the slug in the post editor is no longer editable. I go from

    /postType/editableSlug

    to

    /taxonomyBase/taxon/%taxonomyBase/taxon%

    Which is not editable.

    The filter is thus:

    function taxonPost_link($post_link, $id=0){
      $post = get_post($id);
    
      if( is_object($post) &&
          $post->post_type == 'postType' &&
          $terms = wp_get_object_terms($post->ID, 'taxon') ){
    
          #taxonomyBase/taxon
          $taxa = 'taxonomyBase/'. $terms[0]->slug;
    
          #Replace postType as base with taxonomy, unless we are previewing
          return preg_replace('/(?<!post_type=)\bpostType\b/', $taxa, $post_link);
      }
    
      return $post_link;
    }
    add_filter('post_type_link', 'taxonPost_link', 1, 3);

    I tried using the sample parameter to make the filter a no-op if sample is true, but it never seems to get passed on; there are many errors if the function definition does not include a default value assignment for the parameter.

    [ Please do not bump, that’s not permitted here. ]

  • The topic ‘post_type_link for custom permalink struct of custom types breaks editable slug’ is closed to new replies.