clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Redirect parent page to its first child

Filed under: WordPress— Tagged with: redirect, snippets

Redirecting from the parent page comes really handy, when you don't have actual need for the parent page, expect to group the subpages under it.

Make a new file called page-pagename.php or page-ID.php and paste the following into it.

if (have_posts()) {
  while (have_posts()) {
    the_post();
    $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    if ($pagekids) {
      if (!get_post_meta($post->ID, 'dont_redirect', true)) {
        $firstchild = $pagekids[0];
        wp_redirect(get_permalink($firstchild->ID));
      }
    }
  }
}

Source

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!