title: Wordpress Custom Post Type Permalink with Parent Post Title in URL
body: <p>I just created a custom post type called <strong>'mtl_chapter'</strong>. With my script, I assign a post parent of my CPT to the regular 'post' type. So, my CPT are basically the children of my regular posts. I want to change the permalink structure of my CPT from </p>
  
  <pre><code>/base-slug/cpt-post-title/ 
  </code></pre>
  
  <p>to </p>
  
  <pre><code>/parent-title/cpt-post-title/
  </code></pre>
  
  <p>So, It will look like the attachment posts do with the permalink: </p>
  
  <pre><code>/parent-title/attachment-post-title/
  </code></pre>
  
  <p>My current code is able to change the permalink structure to what I want, but I get </p>
  
  <blockquote>
    <p>404 not found </p>
  </blockquote>
  
  <p>when I click the link. Please help me, here is my current code:</p>
  
  <pre><code>function create_posttype() {
    register_post_type( 'mtl_chapter',
      array(
        'labels' =&gt; array(
          'name' =&gt; 'Chapters',
          'singular_name' =&gt; 'Chapter',
          'parent_item_colon' =&gt; 'Novel Title:',
          'add_new' =&gt; _x('Add New', 'indomtl'),
          'add_new_item' =&gt; __( 'Add New Chapter' )
        ),
        'public' =&gt; true,
        'has_archive' =&gt; true,
        'menu_icon' =&gt; 'dashicons-format-aside',
        'rewrite' =&gt; array('slug' =&gt; '%parent-post-name%','with_front' =&gt; true),
        'exclude_from_search' =&gt; true,
        'show_ui' =&gt; true,
        'menu_position' =&gt; 5
      )
    );
  }
  
  add_action( 'init', 'create_posttype' );
  
  add_filter('post_type_link', 'mtl_update_permalink_structure', 10, 2);
  
  function mtl_update_permalink_structure( $post_link, $post )
  {
      if ( false !== strpos( $post_link, '%parent-post-name%' ) ) {
          $parent_id = wp_get_post_parent_id($post-&gt;ID);
          $parent_post = get_post($parent_id); 
          $slug = $parent_post-&gt;post_name;
          if ( $slug ) {
              $post_link = str_replace( '%parent-post-name%', $slug, $post_link );
          }
  
      }
      return $post_link;
  }
  </code></pre>