3 Additional testing results.
source | link

I am having some issues with rewrite rules. I have a URL like this:

http://www.domain.com/post-type/parent-page/child-page/section-id

Which I want to treat via query vars like

http://www.domain.com/post-type/parent-page/child-page?section=section-id

I have this tag and rule set:

add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
    add_rewrite_tag('%section%', '([^(&)]+)');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?pagename=$matches[1]/$matches[2]&section=$matches[3]','top');
} 

However the page is 404ing, and outputting $wp->matched_query shows that it isn't selecting my rule, but one further down the list of rules (that was auto created by WP). It's actually matching the last rule in the $wp_rewrite->wp_rewrite_rules() array that starts with post-type.

Weird thing is that if I change the rule to:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?p=1234567&extravar=$matches[1]/$matches[2]&section=$matches[3]','top');

(i.e., purposely 404ing the page by going to an ID that doesn't exist, and just passing along the vars for reference) $wp->matched_query shows my regex, and the url is as it should be (index.php?p=1234567&extravar=parent-page/child-page&section=section-id)

What is the reason why my redirect URL is causing the rewrite engine to skip over my rule? Is something up with pagename?

EditEdit: Additional tests have revealed that going directly to http://www.domain.com/post-type/?pagename=parent-page/child-page&section=section-id works. so pagename is apparently valid.

Edit 2: More testing, changed the add_rewrite_rule call to check for post type as well...No Dice:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?post_type=post-type&pagename=$matches[1]/$matches[2]&section=$matches[3]','top');

I have also tried just using $matches[2], still not working:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?post_type=post-type&pagename=$matches[2]&section=$matches[3]','top');

The strange thing still is that building these URLs manually (as in filling in the query string values) actually loads the correct page. Don't know what the Rewrite API would make that any different.

I am having some issues with rewrite rules. I have a URL like this:

http://www.domain.com/post-type/parent-page/child-page/section-id

Which I want to treat via query vars like

http://www.domain.com/post-type/parent-page/child-page?section=section-id

I have this tag and rule set:

add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
    add_rewrite_tag('%section%', '([^(&)]+)');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?pagename=$matches[1]/$matches[2]&section=$matches[3]','top');
} 

However the page is 404ing, and outputting $wp->matched_query shows that it isn't selecting my rule, but one further down the list of rules (that was auto created by WP). It's actually matching the last rule in the $wp_rewrite->wp_rewrite_rules() array that starts with post-type.

Weird thing is that if I change the rule to:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?p=1234567&extravar=$matches[1]/$matches[2]&section=$matches[3]','top');

(i.e., purposely 404ing the page by going to an ID that doesn't exist, and just passing along the vars for reference) $wp->matched_query shows my regex, and the url is as it should be (index.php?p=1234567&extravar=parent-page/child-page&section=section-id)

What is the reason why my redirect URL is causing the rewrite engine to skip over my rule? Is something up with pagename?

Edit: Additional tests have revealed that going directly to http://www.domain.com/post-type/?pagename=parent-page/child-page&section=section-id works. so pagename is apparently valid.

I am having some issues with rewrite rules. I have a URL like this:

http://www.domain.com/post-type/parent-page/child-page/section-id

Which I want to treat via query vars like

http://www.domain.com/post-type/parent-page/child-page?section=section-id

I have this tag and rule set:

add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
    add_rewrite_tag('%section%', '([^(&)]+)');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?pagename=$matches[1]/$matches[2]&section=$matches[3]','top');
} 

However the page is 404ing, and outputting $wp->matched_query shows that it isn't selecting my rule, but one further down the list of rules (that was auto created by WP). It's actually matching the last rule in the $wp_rewrite->wp_rewrite_rules() array that starts with post-type.

Weird thing is that if I change the rule to:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?p=1234567&extravar=$matches[1]/$matches[2]&section=$matches[3]','top');

(i.e., purposely 404ing the page by going to an ID that doesn't exist, and just passing along the vars for reference) $wp->matched_query shows my regex, and the url is as it should be (index.php?p=1234567&extravar=parent-page/child-page&section=section-id)

What is the reason why my redirect URL is causing the rewrite engine to skip over my rule? Is something up with pagename?

Edit: Additional tests have revealed that going directly to http://www.domain.com/post-type/?pagename=parent-page/child-page&section=section-id works. so pagename is apparently valid.

Edit 2: More testing, changed the add_rewrite_rule call to check for post type as well...No Dice:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?post_type=post-type&pagename=$matches[1]/$matches[2]&section=$matches[3]','top');

I have also tried just using $matches[2], still not working:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?post_type=post-type&pagename=$matches[2]&section=$matches[3]','top');

The strange thing still is that building these URLs manually (as in filling in the query string values) actually loads the correct page. Don't know what the Rewrite API would make that any different.

2 Adding results of additional testing.
source | link

I am having some issues with rewrite rules. I have a URL like this:

http://www.domain.com/post-type/parent-page/child-page/section-id

Which I want to treat via query vars like

http://www.domain.com/post-type/parent-page/child-page?section=section-id

I have this tag and rule set:

add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
    add_rewrite_tag('%section%', '([^(&)]+)');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?pagename=$matches[1]/$matches[2]&section=$matches[3]','top');
} 

However the page is 404ing, and outputting $wp->matched_query shows that it isn't selecting my rule, but one further down the list of rules (that was auto created by WP). It's actually matching the last rule in the $wp_rewrite->wp_rewrite_rules() array that starts with post-type.

Weird thing is that if I change the rule to:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?p=1234567&extravar=$matches[1]/$matches[2]&section=$matches[3]','top');

(i.e., purposely 404ing the page by going to an ID that doesn't exist, and just passing along the vars for reference) $wp->matched_query shows my regex, and the url is as it should be (index.php?p=1234567&extravar=parent-page/child-page&section=section-id)

What is the reason why my redirect URL is causing the rewrite engine to skip over my rule? Is something up with pagename?

Edit: Additional tests have revealed that going directly to http://www.domain.com/post-type/?pagename=parent-page/child-page&section=section-id works. so pagename is apparently valid.

I am having some issues with rewrite rules. I have a URL like this:

http://www.domain.com/post-type/parent-page/child-page/section-id

Which I want to treat via query vars like

http://www.domain.com/post-type/parent-page/child-page?section=section-id

I have this tag and rule set:

add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
    add_rewrite_tag('%section%', '([^(&)]+)');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?pagename=$matches[1]/$matches[2]&section=$matches[3]','top');
} 

However the page is 404ing, and outputting $wp->matched_query shows that it isn't selecting my rule, but one further down the list of rules (that was auto created by WP). It's actually matching the last rule in the $wp_rewrite->wp_rewrite_rules() array that starts with post-type.

Weird thing is that if I change the rule to:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?p=1234567&extravar=$matches[1]/$matches[2]&section=$matches[3]','top');

(i.e., purposely 404ing the page by going to an ID that doesn't exist, and just passing along the vars for reference) $wp->matched_query shows my regex, and the url is as it should be (index.php?p=1234567&extravar=parent-page/child-page&section=section-id)

What is the reason why my redirect URL is causing the rewrite engine to skip over my rule? Is something up with pagename?

I am having some issues with rewrite rules. I have a URL like this:

http://www.domain.com/post-type/parent-page/child-page/section-id

Which I want to treat via query vars like

http://www.domain.com/post-type/parent-page/child-page?section=section-id

I have this tag and rule set:

add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
    add_rewrite_tag('%section%', '([^(&)]+)');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?pagename=$matches[1]/$matches[2]&section=$matches[3]','top');
} 

However the page is 404ing, and outputting $wp->matched_query shows that it isn't selecting my rule, but one further down the list of rules (that was auto created by WP). It's actually matching the last rule in the $wp_rewrite->wp_rewrite_rules() array that starts with post-type.

Weird thing is that if I change the rule to:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?p=1234567&extravar=$matches[1]/$matches[2]&section=$matches[3]','top');

(i.e., purposely 404ing the page by going to an ID that doesn't exist, and just passing along the vars for reference) $wp->matched_query shows my regex, and the url is as it should be (index.php?p=1234567&extravar=parent-page/child-page&section=section-id)

What is the reason why my redirect URL is causing the rewrite engine to skip over my rule? Is something up with pagename?

Edit: Additional tests have revealed that going directly to http://www.domain.com/post-type/?pagename=parent-page/child-page&section=section-id works. so pagename is apparently valid.

1
source | link

Rewrite CPT child page URL to support query var

I am having some issues with rewrite rules. I have a URL like this:

http://www.domain.com/post-type/parent-page/child-page/section-id

Which I want to treat via query vars like

http://www.domain.com/post-type/parent-page/child-page?section=section-id

I have this tag and rule set:

add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
    add_rewrite_tag('%section%', '([^(&)]+)');
}

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?pagename=$matches[1]/$matches[2]&section=$matches[3]','top');
} 

However the page is 404ing, and outputting $wp->matched_query shows that it isn't selecting my rule, but one further down the list of rules (that was auto created by WP). It's actually matching the last rule in the $wp_rewrite->wp_rewrite_rules() array that starts with post-type.

Weird thing is that if I change the rule to:

add_rewrite_rule('post-type/(.+?)/(.+?)/(.+?)/?$', 'index.php?p=1234567&extravar=$matches[1]/$matches[2]&section=$matches[3]','top');

(i.e., purposely 404ing the page by going to an ID that doesn't exist, and just passing along the vars for reference) $wp->matched_query shows my regex, and the url is as it should be (index.php?p=1234567&extravar=parent-page/child-page&section=section-id)

What is the reason why my redirect URL is causing the rewrite engine to skip over my rule? Is something up with pagename?