Skip to content

Instantly share code, notes, and snippets.

@dboutote
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dboutote/62e4fac0600895d5b879 to your computer and use it in GitHub Desktop.
Save dboutote/62e4fac0600895d5b879 to your computer and use it in GitHub Desktop.
This checks if a parent page should load its first child page.
add_action( 'parse_request', 'check_for_redirect' );
function check_for_redirect( $query ) {
if( is_admin() ){
return;
}
if( isset($query->query_vars[ 'pagename' ]) ){
$pagename = $query->query_vars[ 'pagename' ];
$parent_redirect_id = (int) get_option('parent_redirect_' . $pagename);
if( $parent_redirect_id > 0 ){
$kids = get_pages("child_of=".$parent_redirect_id."&sort_column=menu_order");
if ($kids) {
$kid = $kids[0];
$newpagename = $pagename . '/' . $kid->post_name;
$query->query_vars[ 'pagename' ] = $newpagename;
}
}
}
return $query;
}
@dboutote
Copy link
Author

dboutote commented Dec 1, 2014

Requires a metabox to indicate that the page should redirect and an option set like so: parent_redirect_{$pagename} with a value of the $pagename 's ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment