Can anyone please help me to with the wp_query.
I am making a template file/loop to create and archive page of the current page children pages.
This query needs to be automatic as I am using in on few pages.
This is my query below, but it just returns my posts instead of child pages.
<?php
$parent = new WP_Query(array(
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => -1
));
if ($parent->have_posts()) : ?>
<?php while ($parent->have_posts()) : $parent->the_post(); ?>
<div id="parent-<?php the_ID(); ?>" class="parent-page">
<h1><a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
<p><?php the_advanced_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php unset($parent); endif; wp_reset_postdata(); ?>
Thanks in advance for any help.
Josh
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You have to change child_of to post_parent and also add post_type => 'page':
WordPress codex Wp_query Post & Page Parameters
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div id="parent-<?php the_ID(); ?>" class="parent-page">
<h1><a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
<p><?php the_advanced_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
Method 2
I know this is a very old question, but since I landed on it, others might as well.
WordPress has a very simple solution for listing pages, where you can add some arguments as well.
This is all you will need to display a page’s children:
wp_list_pages(array( 'child_of' => $post->ID, 'title_li' => '' ))
Look at the reference page for wp_list_pages for all options you can apply.
Method 3
Rewriting this to a function in functions.php you need to add
global $post;
function page_summary() {
global $post;
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div id="parent-<?php the_ID(); ?>" class="parent-page">
<h1><a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
}
Method 4
Though OP asked specifically for a wp_query, this also works:
get_pages('child_of='.$post->ID.'&sort_column=menu_order');
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0