post_parent) and is_numeric($post->post_parent)) { if ($post->post_parent != 0) { wp_redirect(get_permalink($post->post_parent), 301); exit; } else { $publish = true; $path = ''; $sql = "SELECT post_name, post_parent FROM " . $wpdb->posts . " WHERE post_name = '" . $post->post_name . "' AND post_status = 'publish' AND (post_type = 'page' OR post_type = 'post') ORDER BY ID ASC LIMIT 1"; $row = $wpdb->get_row($sql); if ($row) { $path = $post->post_name . '/'; while ($row->post_parent) { $sql = "SELECT post_status, post_name, post_parent FROM " . $wpdb->posts . " WHERE ID = " . $row->post_parent; $row = $wpdb->get_row($sql); if ($row->post_status != 'publish') { $publish = false; break; } $path = $row->post_name . "/$path"; } } if ($path and $publish) { wp_redirect(home_url("/$path"), 301); exit; } else { wp_redirect(home_url(), 302); exit; } } } } add_action('template_redirect', 'aprx_template_redirect', 1); } ?>