Try adding the ‘paged’ parameter to the query:
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
And see the example on how to use paginate_links():
http://codex.wordpress.org/Function_Reference/paginate_links#Examples
Change this
'total' => $wp_query->max_num_pages
to this
'total' => $my_query->max_num_pages
Hi again, sorry for the delay,
I’ve tried combining the code and had very little success, my rudementary debugging shows that $my_query->max_num_pages echoes: 0
<?php
$gen1_ids = get_the_ID();
$gen2 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen1_ids) AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC");
$gen2_ids = implode($gen2,', ');
$gen3 = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen2_ids) AND $wpdb->posts.post_type = 'page' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.ID ASC");
$gen3_ids = implode($gen3,', ');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post__in' => $gen3,
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 2,
// I've set it to 2 as on the test server I only have a few
// examples to play with.
'caller_get_posts'=> 1,
'paged' => $paged
);
$my_query = null;
$my_query = new WP_Query($args);
$big = 999999999; // need an unlikely integer
$grandchildargs = array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
);
?> <div class="pagination"> <?php echo paginate_links( $grandchildargs ); echo $my_query->max_num_pages; ?> </div>
<?php /* WHILE CODE HAPPENS HERE */ ?>
?php endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Is there something I’m missing here?
What happens if you query like this:
<?php
$gen1_ids = get_the_ID(); // try echoing this to see if the id is available
// echo '$gen1_ids = '. $gen1_ids;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'fields' => 'ids',
'post_parent' => $gen1_ids,
'paged' => $paged,
);
// get children post ids
$children_ids = get_posts( $args );
unset( $args['post_parent'] );
// add argument post_parent_in (grandchildren)
$args['post_parent__in'] = $children_ids;
// the query
$my_query = new WP_Query( $args );
?>
Hi keesiemeijer, your query returns a list of all pages, yet echoing $gen1_ids;
returns the page id correctly.
I’m very confused.
I’ve tested it and it returns a list of all pages if the page doesn’t have any child or grandchild pages. Are you sure the page you’re on has child or grandchild pages? You could wrap your query and loop in a conditional if the page doesn’t have any children.
Example:
<?php
$gen1_ids = get_the_ID(); // try echoing this to see if the id is available
// echo '$gen1_ids = '. $gen1_ids;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'fields' => 'ids',
'post_parent' => $gen1_ids,
'paged' => $paged,
);
// get children post ids
$children_ids = get_posts( $args ); ?>
<?php if ( $children_ids ) : ?>
<?php
// remove arguments
unset( $args['post_parent'], $args['fields']);
// add argument post_parent_in (grandchildren)
$args['post_parent__in'] = $children_ids;
// the query
$my_query = new WP_Query( $args );
?>
<!-- pagination here -->
<?php if ( $my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<?php // rest of loop here ?>
<?php endwhile; // end of the loop. ?>
<!-- pagination here -->
<?php else : ?>
<?php _e( 'Sorry, no grandchild pages found.' ); ?>
<?php endif; ?>
<?php else : ?>
<p><?php _e( 'Sorry, no child pages found.' ); ?></p>
<?php endif; ?>
May I ask, why does this:
// add argument post_parent_in (grandchildren)
$args['post_parent__in'] = $children_ids;
have a double underscore? is that a typo?
Not a typo. See: http://codex.wordpress.org/Function_Reference/WP_Query#Post_.26_Page_Parameters
Basically it queries for (grand children) posts that are direct descendants of the post ids in the $children_ids array.
Keesiemeijer, thanks for your support thus far, I really appreciate your time! While I understand the principle of what you’re suggesting I’m failing to get this thing to fall together.
Seacoast Web design, I’m not entirely sure your post was relevant.
From last code suggestion I’ve tried to debug as the query is still returning all pages, despite the parent page (ID 301) only has a bunch of grandchildren. The pagination still doesn’t work either. :/
div id="childprojects" class="container">
<?php
$gen1_ids = get_the_ID(); // try echoing this to see if the id is available
// echo '$gen1_ids = '. $gen1_ids;
//returns $gen1_ids = 301
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
//echo '$paged = '. $paged;
// returns 1
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'fields' => 'ids',
'post_parent' => $gen1_ids,
'paged' => $paged,
);
// get children post ids
$children_ids = get_posts( $args );
/*
echo '<pre>';
print_r($children_ids);
echo '</pre>';
// returns Array([0]=>417)
*/
if ( $children_ids ) : ?>
<?php
// remove arguments
unset( $args['post_parent'], $args['fields']);
// add argument post_parent_in (grandchildren)
$args['post_parent__in'] = $children_ids;
// the query
$my_query = new WP_Query( $args );
?>
<!-- pagination here -->
<?php if ( $my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="child six columns" style="text-align: center;">
<?php // Display Feature Image if there is one to show.
?>
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>" style="text-align:center;"> <h3> <?php the_title(); ?> </h3></a>
</div>
<?php endwhile; // end of the loop. ?>
<!-- pagination here -->
<?php $grandchildargs = array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
); ?>
<div class="pagination"> <?php echo paginate_links( $grandchildargs ); ?> </div>
<?php else : ?>
<?php _e( 'Sorry, no grandchild pages found.' ); ?>
<?php endif; ?>
<?php else : ?>
<p><?php _e( 'Sorry, no child pages found.' ); ?></p>
<?php endif; ?>
</div> <!-- end .childprojects -->
I’ve included as comment the results of the echoed and print_r()’d results.
That’s strange. On my testsite It works how it should.
Does the page with id 417 have any child pages?
Try:
– deactivating all plugins to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).
Hi keesiemeijer, all plugins deactivated still same problem.
Page ID 417 (on the test site) is the child page with grandchildren (which is correct).
I’m confused, what would cause the all pages to be listed during the while instance? Surely, the query wouldn’t return them unless their parent was 301 (which it isn’t). I’m flummoxed!
In an endeavour to debug this I’ve counted the times the while instance cycles. The number should be somewhere in the region of 5 (5 grandchildren pages on test site).
<?php $i=1; while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<pre>
<?php echo $i .' cycle'; print_r($my_query); $i++; ?>
</pre>
<?php endwhile; // end of the loop. ?>
However, the net result is all pages (published) are returned (15 cycles).
Can you post the full code of that template file in a pastebin.
See: http://codex.wordpress.org/Forum_Welcome#Posting_Code
*edit to pastebin*
Pastebin link:
http://pastebin.com/RH3nhdSt
Many thanks!