Why isn’t my rewrite rule working when there is no second parameter?

Your regular expression is requiring the last slash be present which it will not be by default. So the path tides/mylocation/ is shortened to tides/mylocation and then tested. Instead, wrap the last part in an optional group using the ? and update the match number. add_rewrite_rule(‘tides/([^/]+)(/([^/]+))?’, ‘index.php?page_id=4348&location=$matches[1]&month=$matches[3]’, ‘top’);

Combine multiple CPT names to create valid permalinks

Sally CJ commented an important detail that helped me reach what I needed. Apart from that I struggled with what seemed to be regex error – one of my rules was overriding another because there was no $ character in regex to end the string. Working solution for custom rewrites: function dd_custom_rewrite_rules() { add_rewrite_tag(‘%company%’, ‘([a-z0-9-]+)’, … Read more

Custom Post Type posts, conflict with posts after add_rewrite_rule to top

I think I solved my problem. So if you have your WordPress post permalink set to %category%/%postname%/, and you want this for custom post type and taxonomy, you could prepare your rewrite links for all generated taxonomy terms (cat-a, cat-b etc.). Code: add_filter( ‘rewrite_rules_array’, function($rules) { $categories = get_terms([‘taxonomy’ => ‘success-category’ ]); foreach ($categories as … Read more