WP redirects pretty permalink to query string

I have a custom post type artist and a taxonomy kind_of_art.
I want their permalinks to be like:

%kind_of_art%/%artist%

Read More

Therefore I created following rewrite rules:

  add_rewrite_rule(
    '([^/]+)/([^/]*)/?',
    'index.php?kind_of_art=$matches[1]&post_type=artist&artist=$matches[2]',
    'top'
    );

If I go to photography/sam-sample for example, I am redirected to ?artist=sam-sample.
If I add a front base to my rewrite rule it works.

Additional info

An artist post consists of multiple textareas with different content. I use this technique to create virtual subpages (e.g. press, gallery, biography). Of course I need rewrite rules again:

  add_rewrite_tag('%artist_sub_page%', '([^&]+)');
  add_rewrite_rule(
    '([^/]+)/([^/]*)/([^/]*)',
    'index.php?artist=$matches[2]&artist_sub_page=$matches[3]&kind_of_art=$matches[1]',
    'top'
  );

Funnily enough, this works even without that front base.

Any ideas whats wrong? I assume this is an easy issue, but I am no expert in rewrite rules.

EDIT:

I just discovered two things:

If I visit my /photography/sam-sample/ I will be redirected to ?artist=sam-sample. But if I enter my wanted URL again, I am not redirected and it “works”. I guess this is why I already thought a few times that I got it working but a minute later it wasn’t 😉

If I visit append any string to my wanted URL structure – e.g. photography/sam-sample/foo or photography/sam-sample/foo/bar – I am also not redirected.

EDIT #2:

I got it working now with this regex:

([^/]+)/([^/]*)

By working I mean I can visit /photography/sam-sample and it will work. One problem to solve still:

In my navigation I create the links dynamically with get_permalink(). This function still outputs ?artist=sam-sample instead my wanted structure.

Related posts