• Hey guys,

    I’ve been trying to retrieve a list of grandchild pages, using a post_thumnail to make a gallery of said pages. However, I wish this list to be limited to a comfortable amount per page.

    Having used successfully the code found here: http://wordpress.org/support/topic/query_posts-grandchildren-of-static-pages?replies=3

    <?php
    $gen1_ids = 56;
    $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,', ');
    $args=array(
      'post__in' => $gen3,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1, //  I wish to control the posts items listed
      'caller_get_posts'=> 1  //  I also wish to paginate
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Pages grandchild pages of id 56' ;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    
    ?>

    I’m now struggling to paginate as I’m wanting to set the posts_per_page to a more manageable number.
    <div class="pagination"> <?php echo paginate_links( ); echo $paged;?> </div> (returns nothing)

    Having read: http://wordpress.stackexchange.com/questions/21181/custom-post-type-archive-page-set-posts-per-page-paginate I’m none the wiser how to resolve this issue.

    Any ideas?

Viewing 15 replies - 1 through 15 (of 29 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    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

    Removed.

    Thread Starter Neilisin

    (@neilisin)

    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?

    Moderator keesiemeijer

    (@keesiemeijer)

    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 );
    ?>

    Thread Starter Neilisin

    (@neilisin)

    Hi keesiemeijer, your query returns a list of all pages, yet echoing $gen1_ids; returns the page id correctly.

    I’m very confused.

    Moderator keesiemeijer

    (@keesiemeijer)

    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; ?>

    Thread Starter Neilisin

    (@neilisin)

    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?

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter Neilisin

    (@neilisin)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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).

    Thread Starter Neilisin

    (@neilisin)

    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!

    Thread Starter Neilisin

    (@neilisin)

    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).

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you post the full code of that template file in a pastebin.
    See: http://codex.wordpress.org/Forum_Welcome#Posting_Code

    Thread Starter Neilisin

    (@neilisin)

    *edit to pastebin*

    Pastebin link:

    http://pastebin.com/RH3nhdSt

    Many thanks!

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘Custom Query, show grandchild pages… paginating?!’ is closed to new replies.