0

I want to use a hierarchical (nested) page with a query var to pull dynamic content. I had things working when the page wasn't a child of another page, such as:

mysite.com/my-page/1234-my-var/

However, when I made my page a child of another page, my query_var no longer works, e.g.

mysite.com/parent-1/parent-2/my-page/1234-my-var/

My code

/* Register Query Var
----- */
function my_queryvar_params( $query_v ) {
    $query_v[] = "my_var";
    return $query_v;
};
add_filter('query_vars',  'my_queryvar_params');
/* Rewrites
----- */
function setup_filter_rewrites(){
    //add_rewrite_rule('^my\-page\/([0-9]+)\/?', 'index.php?pagename=my-page&my_var=$matches[1]', 'top'); // This works when my page has no parent, Returns a 404 when my page is a child/nested
    add_rewrite_rule('(.*)\/my\-page\/([0-9]+)\/?', 'index.php?pagename=my-page&my_var=$matches[1]', 'top'); // This doesn't return a 404, but I can't fetch my query_var using get_query_var()
    //add_rewrite_rule('^parent\-1\/parent\-2\/my\-page\/([0-9]+)\/?', 'index.php?pagename=my-page&my_var=$matches[1]', 'top'); // This doesn't return a 404, but I can't fetch my query_var using get_query_var()
};
add_action( 'init', 'setup_filter_rewrites' );
/* Force Redirect
----- */
function my_redirect_check() {
    if (isset($_GET['my_var'])) {
        if ($_GET['my_var'] != '') {
            if ($_SERVER["HTTPS"]) {$location = 'https://';}
            else {$location = 'http://';}
            $location .= $_SERVER['SERVER_NAME'];   
            $location .= strtok($_SERVER['REQUEST_URI'],'?');
            $location = trailingslashit($location);
            $location .= $_GET['my_var'];
            $location = trailingslashit($location);                          
            wp_redirect($location); exit;
        };
    };
};
add_action('init','my_redirect_check');

I'm not sure if my regex is wrong (I have run it through a regex checker and it checks out) or if I'm just missing something obvious.

Would really appreciate any insights!

  • 2
    pagename has to reflect parent/child path because duplicate slugs are allowed if they have different parents. – Milo Apr 30 '17 at 20:16
1

Just in case any one needs this:

/* Register Query Var
----- */
function my_queryvar_params( $query_v ) {
    $query_v[] = "my_var";
    return $query_v;
};
add_filter('query_vars',  'my_queryvar_params');
/* Rewrites
----- */
function setup_filter_rewrites(){
    add_rewrite_rule('^my-parent/my-page\/([0-9]+)\/?', 'index.php?pagename=my-parent/my-page&my_var=$matches[1]', 'top'); // As @Milo mentioned, the pagename paramenter needs to reflect the path hierarchy
};
add_action( 'init', 'setup_filter_rewrites' );
/* Force Redirect
----- */
function my_redirect_check() {
    if (isset($_GET['my_var'])) {
        if ($_GET['my_var'] != '') {
            if ($_SERVER["HTTPS"]) {$location = 'https://';}
            else {$location = 'http://';}
            $location .= $_SERVER['SERVER_NAME'];   
            $location .= strtok($_SERVER['REQUEST_URI'],'?');
            $location = trailingslashit($location);
            $location .= $_GET['my_var'];
            $location = trailingslashit($location);                          
            wp_redirect($location); exit;
        };
    };
};
add_action('init','my_redirect_check');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.