title: Rewrite custom post type url with another custom post type
body: <p>I have two custom post types <code>Location</code> and <code>House</code>. </p>
  
  <p>I am trying to rewrite the the url of house post type to <code>/location/%LocationName%/house</code></p>
  
  <p>I've used <code>add_filter('post_type_link', 'postTypeLink', 13, 3)</code> to replace <code>%LocationName%</code> and the permalink did change to the format I want, however when I click on the link, it only throws out a page not found error. </p>
  
  <p>I have printed the <code>wp_query</code> and found out wp is still trying to do a <code>Location</code> query but with a name <code>%LocationName%/house</code></p>
  
  <p>e.g. House has a post <code>yellow</code>. So, the url domain.com/location/london/house/yellow will make a location query with name <code>/london/house/yellow</code> instead of a house query with name <code>yellow</code>. it caused page not found as location doesn't have any such post.</p>
  
  <p>I've tried to use <code>add_rewrite_rule</code> or save the permalink but it only led to the same result.</p>
  
  <p>The code is below.</p>
  
  <pre><code>    add_action( 'init', 'rewrite_tag', 10, 0 );
  
      function rewrite_tag() {
         add_rewrite_tag( '%locationName%', '([^/]*)/?$', 'locationName=' );
         add_rewrite_rule(
             '^location/([^/]*)/?$/house',
             'index.php?post_type=house&amp;locationName=$matches[1]',
             'top');
         flush_rewrite_rules(false);
      }
  
      $args = array(
          'labels' =&gt; $labels,
          'description' =&gt; 'Manage Research Publication',
          'public' =&gt; true,
          'publicly_queryable' =&gt; true,
          'show_ui' =&gt; true,
          'show_in_menu'  =&gt; true,
          'query_var' =&gt; true,
          'rewrite' =&gt; array(
                  'slug' =&gt; 'location/%locationName%/house',
                  'with_front'=&gt; false,  
                  'hierarchical' =&gt; true
          ),
          'has_archive' =&gt; false,
          'hierarchical' =&gt; true,
          'menu_icon' =&gt; 'dashicons-clipboard',
          'menu_position' =&gt; null,
          'supports' =&gt; array('title', 'editor', 'thumbnail'),
          'show_in_rest'          =&gt; true,
          'rest_base'             =&gt; 'house',
          'taxonomies' =&gt; array('category'),
          'rest_controller_class' =&gt; 'WP_REST_Posts_Controller',
  
      );
      register_post_type( 'house', $args );
  
      add_filter('post_type_link', 'postTypeLink', 13, 3);
      function postTypeLink($link, $post)    {
        global $post;
         if (get_post_type($post) === 'location') {
  
             $location = a variable from global post
             $link = str_replace('%LocationLName%', $location, $link);
  
  
         }
         return $link;
      }
  </code></pre>