title: WordPress and PHP: Removing Parent Slug from Child Page Permalink
body: <p>I feel like I'm very close with the code I have so far, but something is still not working. None of the other similar questions on Stack Overflow solve this issue.</p>
  
  <p>I have parent pages and child pages (not posts, not custom post type) in WordPress, and the default mydomain.com/%postname% structure selected.</p>
  
  <p>Currently the permalink structure for pages is mydomain.com/parent-page/child-page</p>
  
  <p>In the backend on the single page editor, the desired permalink displays correctly (with the parent page removed). i.e. mydomain.com/child-page</p>
  
  <p>However when I visit mydomain.com/child-page I get a 404.</p>
  
  <p>I have flushed rewrite rules by visiting the permalink settings page already.</p>
  
  <p>What am I doing wrong?</p>
  
  <pre><code> function wpse_101072_flatten_hierarchies( $page_link, $post ) {
      $post = get_post($post);
  
      $uri = '';
  
      if($post-&gt;post_parent !== 0) {
          $uri = get_post( $post-&gt;post_parent )-&gt;post_name . "/" . $uri;
          return str_replace( $uri, '', $page_link );
      } else {
          return $page_link;
      }
  }
  add_filter( 'page_link', 'wpse_101072_flatten_hierarchies', 10, 2 );
  
  function wp_pages_permalink( $permalink_structure, $post_id ) {
      if ( empty( $post_id ) ) return $permalink_structure ;
  
      $post = get_post( $post_id );
  
      if($post-&gt;post_parent !== 0) {
          $par = get_post($post-&gt;post_parent);
          $parname = $par-&gt;post_name . '/';
          $permalink_structure = str_replace( '%pagename%', $post-&gt;post_name, $permalink_structure);
          return $permalink_structure;
      }
  }
  
  add_filter( 'page_link', 'wp_pages_permalink', 10, 2 ); 
  </code></pre>