Support » Fixing WordPress » Redirecting Pages

  • I have 5 pages on my WP install, and each of the pages has 4-6 child pages.

    On clicking a link for one of the pages I want it to redirect to its first child.

    I’m using this code to find the first child link
    $subPage = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_parent = " . $pageID . " ORDER BY menu_order");

    I thought I’d try using wp_redirect($subPage)
    but it’s not working

    Is there any better way of doing this? I’m sure I’m overlooking something simple

Viewing 4 replies - 1 through 4 (of 4 total)
  • hmmm, maybe replace
    wp_redirect($subPage) with header("Location:{$subPage}");

    does your code work with permalinks too?
    and what about pages that have no subpages?

    and most important: where do i put your code? because i need this feature as well !

    this is more elegant than a plugin, but if you’re stuck try this one


    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    If I wanted to redirect pages to external URLs, I’d use .

    However, given that you want to redirect to another page (the first child), I’d use this:

    $pagekids = get_pages("child_of=$pageID");
    $firstchild = $pagekids[0];

    You might need to use $firstchild['ID'] instead, I’m uncertain there.

    which is the correct synthax? i’ve tried the last one but doesnt’ work..

    In ver 2.6.2, I’m using the following php code to perform this redirect:

    // redirect from empty category pages to first child
    if ( is_page( array('page-slug1', 'page-slug2')) ) {
       $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
       $firstchild = $pagekids[0];

    Note that this code block should be placed at the top of your header.php file.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Redirecting Pages’ is closed to new replies.