add_rewrite_rule() not playing nice with child pages

function my_add_rewrite_rules() {

  add_rewrite_rule('testing/([^/]+)?$', 'index.php?pagename=testing&event_slug=$matches[1]', 'top' ); 

}
add_action( 'init', 'my_add_rewrite_rules' );

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

/testing/foo – Works, shows foo

If I have testing set to no parent, the rewrite works and everything is good to go.

If I set testing to a child of, lets say about, and update the add_rewrite_rule to..

add_rewrite_rule('about/testing/([^/]+)?$', 'index.php?pagename=testing&event_slug=$matches[1]', 'top' );

… it loads about/testing but strips out my custom query_var and displays the page normally.

/about/testing/foo – Loads page, doesn’t show foo

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

if it’s a child page, you have to update pagename to reflect the parent/child path:

add_rewrite_rule('about/testing/([^/]+)?$', 'index.php?pagename=about/testing&event_slug=$matches[1]', 'top' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x