• Hi all,

    I’m stuck and really could use some help! After a lot of research and feedback on several forums, I found a way to remove the slug of my custom post type from my URL. I am using a plugin to add my custom taxonomies into my permalinks. So my permalinks are now right: /%county%/%postname%/ where %county% is a custom taxonomy.

    Now to stop getting a 404 I need a redirect rule and I am so close it is painful! But I can’t figure it out even after reading the codex on wp_rewrite. Here is my full code that gets the slug out, registers the custom taxonomy to all other taxonomies (which I’m not sure I need), and stop the 404: https://hastebin.com/ceruxiqehu.coffee. But the 404 is the part I am not getting, this part below:

    add_filter('generate_rewrite_rules', 'my_rewrite', 9);
     function my_rewrite($wp_rewrite)
     {
        $wp_rewrite->rules = array_merge(array(
            '^([^-]*)-county-([^/]*)/([^/]*)/?$' => 'index.php?custom_taxonomy_slug=county?post_type=county-job'
        ), $wp_rewrite->rules);
    }
    
    //No longer use links with /county-job/ as prefix
    add_filter('post_type_link', 'my_post_link', 1, 3);
    function my_post_link($post_link, $id = 0) {
      $post = get_post($id);
      if(is_object($post) && $post->post_type == 'job') {
          return str_replace('county-job/', '', $post_link);
      }
      return $post_link;
    }

    I think it’s that line: '^([^-]*)-county-([^/]*)/([^/]*)/?$' => 'index.php?custom_taxonomy_slug=county?post_type=county-job'

    I have been messing with it for over an hour! I can’t find anything online about rewriting a custom post type slug and custom post type taxonomy to go to the post! Right now this redirects to BOTH posts. So I know it’s doing something, and I know it’s close, but no dice!

    Any help would be super appreciated, I’m about to bash my head into a wall!

  • The topic ‘Rewrite rules for my custom post types’ is closed to new replies.