Skip to content

Instantly share code, notes, and snippets.

@iamtyce
Created August 10, 2013 06:44
Show Gist options
  • Save iamtyce/6199380 to your computer and use it in GitHub Desktop.
Save iamtyce/6199380 to your computer and use it in GitHub Desktop.
WordPress child page query
<?
// Get page data
$post_id = $post->ID;
$page_data = get_page($post_id);
$page_parent = $page_data->post_parent;
$page_template = get_field('template');
// Count how many children belong to this page
$children_args=array(
'sort_column' => 'menu_order',
'orderby' => 'menu_order',
'order' => 'DESC',
'post_parent' => $post_id,
'post_type' => 'sponsors'
);
$children_query = null;
$children_query = new WP_Query($children_args);
if( $children_query->have_posts() ) {
while ($children_query->have_posts()) : $children_query->the_post();
$children_count++;
$child_id = $post->ID;
endwhile;
}
// Establish if post has 1 child
if (($page_template == "Blank") && ($children_count == 1)) {
$final_redirect = get_permalink($child_id);
wp_redirect($final_redirect);
exit;
//
} elseif (($page_template == "Blank") && ($page_parent != 0)) {
$final_redirect = get_permalink($child_id);
wp_redirect($final_redirect);
exit;
}
wp_reset_query();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment