• 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: https://www.ads-software.com/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: https://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 14 replies - 16 through 29 (of 29 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    I’ve tried it with your code and it still works for me. Not sure what the problem can be.

    Can you print out $my_query just after the query and post it in a pastebin:

    <?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 );
    
    echo '<pre>';
    print_r($my_query);
    echo '</pre>';
    ?>

    Thread Starter Neilisin

    (@neilisin)

    Moderator keesiemeijer

    (@keesiemeijer)

    I see it doesn’t use the query at all. Now I’m confused. If it’s not a plugin it must be your theme that is filtering the query.

    Can you make a backup of the default theme and switch to it. Apply the code to page.php and publish a post with the default template. See if that makes any difference.

    Thread Starter Neilisin

    (@neilisin)

    keesiemeijer, Hi I’ve tried copying and pasting that code into twentytwelve and does exactly the same.

    I’m wondering could the error be in what I’m calling during the while?

    I’m running my test server using xampp on windows 7, could it be related to that?

    I’m truly stumped on this one.

    Moderator keesiemeijer

    (@keesiemeijer)

    I’m truly stumped on this one.

    Me too.

    Put the backed up page.php file back in the default theme and switch back to your own theme.

    This might sound silly but try renaming the WP_Query object:

    <?php
    // remove arguments
    unset( $args['post_parent'], $args['fields']);
    
    // add argument post_parent_in (grandchildren)
    $args['post_parent__in'] = $children_ids;
    
    // the query
    $grand_child_query =  new WP_Query( $args );
    ?>
      <!-- pagination here -->
      <?php if ( $grand_child_query->have_posts() ) : ?>
    
        <?php $i=1; while ( $grand_child_query->have_posts() ) : $grand_child_query->the_post(); ?>

    Did the code from the first post in this topic show the right posts (without pagination)?

    Thread Starter Neilisin

    (@neilisin)

    Heh… this is getting silly now. I’ve replaced page.php with the one from twentytwelve and changed $my_query() with $grand_child_query() and still returning ALL published pages in a list.

    Moderator keesiemeijer

    (@keesiemeijer)

    Did the code from the first post in this topic show the right posts (without pagination)?

    Thread Starter Neilisin

    (@neilisin)

    Heh! Well the code I came with at the start works fine, just no pagination. :/

    Pastebin link

    Moderator keesiemeijer

    (@keesiemeijer)

    Well, let’s use that and try to add the pagination.

    Revert to the post where you combined the code (4th post).
    And remove this:

    $gen3_ids = implode($gen3,', ');

    Thread Starter Neilisin

    (@neilisin)

    That’s not done much.. but thank you for your patience.

    https://pastebin.com/rbDbx87v

    Moderator keesiemeijer

    (@keesiemeijer)

    I’m sorry but I don’t know the solution to your problem. I will think it over some more and maybe something will pop up. It seems new WP_Query doesn’t work for you. We probably missed something trivial. What we’re missing I don’t know. I will ask around.

    Thread Starter Neilisin

    (@neilisin)

    Not to worry, I’ve been beating my brain around about this one.

    The $my_query dump on /page/2/ returns this:

    $my_query dump (page 2)

    My post was to show a method to achieve the intended results using other methods. More than one way to do this. It is relevant, I believe in that I have achieved working pagination of grand-child categories in an archives template. Is that not what you are attempting to achieve?

    Thread Starter Neilisin

    (@neilisin)

    Seacoast Web Design, not quite that, I was after displaying a “gallery” (feature image) of all grandchildren pages.
    The structure I have is:

    • Projects
    • category-type-1
    • project-1
    • project-3
    • project-4
    • category-type-2
    • project-2
    • project-5
    • project-6

    And the desired outcome at the bottom of page “Projects” is to display a gallery of each project irrespective of “category-type”. Convoluted, sure!

    keesiemeijer: I discovered the fault!!
    I had this in my functions.php (after commenting it out it fixed it):
    https://codex.www.ads-software.com/Making_Custom_Queries_using_Offset_and_Pagination#Offset_.26_Manual_Pagination

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