Custom permalinks rules don’t match and lost of query var in the process

For my theme i created custom post types (realisation, video).

All is working now without the rewriting, but i have trouble when i apply rewriting.

Read More

my urls are like this :

1) ?realisation=habillage-de-stands&parentid=38 // for custom post type 'realisation'
2) ?video=jardin-d-ailleurs&parentid=visite-jardin // for custom post type 'video'
3) ?video_category=institutionnel&parentid=pedagogique //for custom tag 'video_category' associated to custom post type 'video'
4) ?p=31&parentid=34 //for blog article
5) ?tag=site-web&parentid=34 //for tags

I tried in permalink

http://127.0.0.1/CONSTRUCTION/wordpress/%postname%/

and i added in functions.php

function add_query_vars($query_vars)
{
    $query_vars[] = "parentid";
    return $query_vars;
}
add_filter('query_vars', 'add_query_vars');

function add_parentid_rewrite()
{
    add_rewrite_rule('^realisation/([^/]*)/([^/]*)', 'index.php?realisation=$matches[1]&parentid=$matches[2]', 'top');
    add_rewrite_rule('^video/([^/]*)/([^/]*)', 'index.php?video=$matches[1]&parentid=$matches[2]', 'top');
    add_rewrite_rule('^video_category/([^/]*)/([^/]*)', 'index.php?video_category=$matches[1]&parentid=$matches[2]', 'top');
}
add_action( 'init', 'add_parentid_rewrite' );

and i have

?realisation=habillage-de-stands&parentid=38

becomes

realisation/habillage-de-stands/&parentid=0/

the link is working but i wish to have

realisation/habillage-de-stands/38/

also

?video=jardin-d-ailleurs&parentid=visite-jardin

becomes

video/jardin-d-ailleurs/&parentid=visite-jardin/

instead of

video/jardin-d-ailleurs/visite-jardin

and also

?video_category=institutionnel&parentid=pedagogique

becomes

video_category/institutionnel/&parentid=&parentid=pedagogique //width 2 &parentid= !!but the link is working

instead of

video_category/institutionnel/pedagogique

=> what is wrong ? and why &parentid= stays in the rewrite link ?

But

?p=31&parentid=34

becomes

article2/&parentid=0

and doesn’t work

=> What could be done here for this ?

The parentid is well taken into account when no rewrite is done (by doing a simple echo $wp_query->query_vars[‘parentid’];). But with the rewrite, the value is messed up!

for instance :

?p=31&parentid=34

gives me empty string with rewrite

?realisation=habillage-de-stands&parentid=38

gives me the string ‘&parentid=0’

Thanks in advance

— EDIT —
if i var_dump($wp_query); in header.php, i’ve got in my results
– when permalinks ARE NOT rewritten :

["query_vars"]=>
  array(58) {
    ["realisation"]=>
    string(19) "habillage-de-stands"
    ["post_type"]=>
    string(11) "realisation"
    ["name"]=>
    string(19) "habillage-de-stands"
    ["parentid"]=>
    string(2) "38"
...
   }
 ["query"]=>
  array(4) {
    ["realisation"]=>
    string(19) "habillage-de-stands"
    ["post_type"]=>
    string(11) "realisation"
    ["name"]=>
    string(19) "habillage-de-stands"
    ["parentid"]=>
    string(2) "38"
  }

when permalinks ARE rewritten :

["query_vars"]=>
array(58) {
    ["page"]=>
    int(0)
    ["realisation"]=>
    string(31) "habillage-de-stands/&parentid=0"
    ["post_type"]=>
    string(11) "realisation"
    ["name"]=>
    string(9) "parentid0"
    ["pagename"]=>
    string(9) "parentid0"
   ["parentid"]=>
   string(11) "&parentid=0"
    ...
}
["query"]=>
array(4) {
    ["realisation"]=>
    string(31) "habillage-de-stands/&parentid=0"
    ["post_type"]=>
    string(11) "realisation"
    ["name"]=>
    string(31) "habillage-de-stands/&parentid=0"
}

Does anyone has a clue ?

Related posts

Leave a Reply