title: Wordpress custom post type dynamic permalink Issue 404 page on other links
body: <p>I am creating plugin and need to add option for custom permalink for custom post type.
  Here are the Permalink Structure options for custom post type user can choose :<a href="https://i.stack.imgur.com/tUWWe.png" rel="nofollow noreferrer">Permalink Structure Options</a></p>
  <p>I already added the below code when registering the custom post type.
  $post_permalink_str is the option I am getting from settings option.</p>
  <pre><code>add_action( 'init', 'locations_post_type' );
  public function locations_post_type() {
  
      $rewrite_slug = 'location';
      if($post_permalink_str === 'base') {
          $rewrite_slug = 'location-page-slug-from-settings'; // This would be page slug selected from settings page. (dynamic)
      } else if($post_permalink_str === 'advanced') {
          $rewrite_slug = 'location/%category%';
      } else if($post_permalink_str === 'reduced') {
          $rewrite_slug = '%category%';
      } else if($post_permalink_str === 'clean') {
          $rewrite_slug = '/';
      }
      register_post_type( 'locations' array(
          'labels' =&gt; array(
              'name' =&gt; _x( 'Locations', 'Post Type General Name', 'domain' ),
          ), 
          'taxonomies' =&gt; array( 'location_categories' ),
          'hierarchical'        =&gt; false,
          'public'              =&gt; true,
          'show_ui'             =&gt; true,
          'show_in_menu'        =&gt; true,
          'show_in_nav_menus'   =&gt; true,
          'show_in_admin_bar'   =&gt; true,
          'has_archive' =&gt; false,
          'rewrite'  =&gt; array(
              'with_front' =&gt; false,
              'slug' =&gt; $rewrite_slug,
          ),
      ));
  
      register_taxonomy(
          'location_categories',
          'locations',
          array(
              'label' =&gt; __( 'Categories', 'domain'),
              'rewrite' =&gt; array( 'slug' =&gt; 'location_categories'),
              'capabilities' =&gt; array(
                  'assign_terms' =&gt; 'manage_options',
                  'edit_terms' =&gt; 'manage_options'
              ),
              'hierarchical' =&gt; true,
              'default_term' =&gt; array(
                  'name' =&gt; __( 'Uncategorized', 'domain'), 
                  'slug' =&gt; 'uncategorized',
              )
          ),
      );
  
      if ( get_option( 'custom_domain_flush_rewrite_rules' ) === 'yes' ) {
          update_option( 'custom_domain_flush_rewrite_rules', 'no' );
          flush_rewrite_rules();
      }
  }
  </code></pre>
  <p>and added this for post link:</p>
  <pre><code>add_filter( 'post_type_link', 'locations_post_link', 1, 3 );
  function locations_post_link( $post_link, $post ) {
      if ( $post-&gt;post_type === 'locations' ) {
          if ( $cats = get_the_terms( $post-&gt;ID, 'location_categories' ) ) {
              $post_link = str_replace( '%category%' , current( $cats )-&gt;slug, $post_link );
          }
      }
      return $post_link;  
  }
  </code></pre>
  <p>The issue is that the structure I am saving from settings are working fine for all cases, for last 3 cases which are &quot;/&quot;, &quot;%category%&quot;, &quot;location/%category%&quot;
  when I set any one from these, the custom post page archive link (single custom post page) is working fine too but other WordPress pages and post showing 404.
  How can I achieve that without conflicting any other pages and posts of WordPress
  I would appreciate if anyone can help me.
  I can provide more details If the above details are not clear to anyone</p>