1

There seem to be countless custom query pagination questions on here, so glad to know I'm not the only one struggling with it. Have tried following the answers in those questions but so far no luck.

I have a custom post type, say Galleries. Each post has a title, and an Advanced Custom Field Gallery field that includes images. When on one of those posts currently it shows the linked titles for the first 12 posts to the right of the images. This means you never get to see past the 12th. I could change the 12 to -1 to show all, but list would go on for a while.

Hoping to add pagination, so if you are on the first post, under the list on the right would said, "more" and would take you to the 13th post, and the list on the right would now show linked titles for 13 to 24. And there would be a prev button and a more button. Prev would go back to first page and show 1 12 titles, and more would take you to 25th post and show 25 to 36 and so on.

I've tried adding pagination in various way to accomplish that but with no luck. Some questions I thought looked right but gave errors. Is pagination the right way, or should I be developing a custom if else statements or such to accomplish this?

<?php get_header(); ?>      

  <div id="main">

    <div id="gallery-viewer">
      <h3><?php the_title(); ?></h3>
        <?php $images = get_field('gallery_images');
        if( $images ): ?>
          <ul class="thumbs">
            <?php foreach( $images as $image ): ?>
              <li>
                <a href="<?php echo $image['url']; ?>">
                  <img src="<?php echo $image['sizes']['gallery-landscape-thb']; ?>" alt="<?php echo $image['alt']; ?>" />
                </a>
              </li>
            <?php endforeach; ?>
          </ul>
        <?php endif; ?>

      <div class="places">
        <ul class="plist selected first">
          <?php $custom_query = new WP_Query(
            array(
              'post_type' => 'customers_gallery',
              'posts_per_page' => 12,
              'paged' => $paged
            )
         );
        if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile;  ?>
        <?php endif; wp_reset_query(); ?>
        </ul>
         // Add Pagination
       </div>
     </div>

   </div><!-- /#main -->

 <?php get_footer(); ?>
  • @Nicolai Thanks for your reply. Looked over your other question, but doesn't seem to apply to my question since not using attachment galleries or anything. But thanks though! – cchiera Sep 10 '17 at 19:44
  • While still not seeing how that example relates to my particular question, if you believe it does and have an example of how it will apply to my code, feel free to post an answer here and can definitely take a look. Thanks! – cchiera Sep 10 '17 at 20:57
  • Would this not be better done by modifying the main query instead of creating a completely new one? – Tom J Nowell Sep 11 '17 at 0:00
  • 1
    Where is your $paged defined? – Annapurna Sep 11 '17 at 12:20
  • 1
    If it's a secondary query, why not paginate it with Ajax? it seems strange to have primary content remain the same while pagination for something else. – Milo Sep 12 '17 at 13:17
0

try this:

$the_query = new WP_Query( array('posts_per_page'=>12,
                                 'post_type'=>'customers_gallery',
                                 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                            ); 
                            ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

  //YOUR CONTENT YOU WANT TO SHOW

<?php
endwhile;

$big = 999999999; // need an unlikely integer
 echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );

wp_reset_postdata();
  • Thanks for your response. Added and pagination appears but the links generated don't work. If I'm on example.com/gallery/dogs/ Clicking next for instance links to example.com/gallery/dogs/page/2/ which then automatically redirects back to example.com/gallery/dogs/ – cchiera Sep 13 '17 at 22:55
  • What if you make a archive-gallery.php? – Dionoh Sep 14 '17 at 20:35
0

Simply use this function:

function wpse279455_query_pagination( $the_query, $pagination_args = array() ) {

    global $wp_query;

    // Make backup of the query object
    $original_query = $wp_query;

    // Swap the global query with our custom qyert
    $wp_query = $the_query;

    // Call the pagination function while custom query is set
    the_posts_pagination( $pagination_args );

    // Restore the query from backup
    $wp_query = $original_query;

}

When you have a new WP_Query object, just pass it on like this:

// My awesome query
$my_query = new WP_Query( array(
    'post_type' => 'my_cpt',
    'posts_per_page' => 10,
) );

// Show the pagination for this query
wpse279455_query_pagination( $my_query, array(
    'mid_size' => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
) );
  • Thanks for your response. Added and pagination appears but the links generated don't work. If I'm on example.com/gallery/dogs/ Clicking next for instance links to example.com/gallery/dogs/page/2/ which then automatically redirects back to example.com/gallery/dogs/. Where is if post per page is set 10, I need the next button to take me to page 11, and this listing of page titles would list titles for 11-21. From my research I'm pretty sure the solution will need to involve the wordpress offset feature and some sort of if statements. So far haven't found the solution on SO. Thanks! – cchiera Sep 13 '17 at 23:02
0

I have not tested this, but you can try

$paged = get_query_var('cpage') ? get_query_var('cpage') : 1;
$custom_query = new WP_Query(
                               array(
                                       'posts_per_page'=>12,
                                       'post_type'=>'customers_gallery',
                                       'paged' => $paged
                               ) 
                            ); 

...

echo paginate_links(
                      array(
                              'format' => '?cpage=%#%',
                              'current' => $paged,
                              'total' => $custom_query->max_num_pages
                           )
                   );

The idea is using a query variable that won't be handled automatically by WP, which I think is what is causing the redirect - since it is a single post that is displayed in one page rather than an archive page.

0

This is what I used for my custom posts type and pagination. This code appears in the single template for the custom post type. In your case, this would be named single-customers-gallery.php

$original_query = $wp_query;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$query = new WP_Query(array(
    'order'       => 'DESC',
    'orderby'     => 'date',
    'post_status' => 'publish',
    'post_type'   => 'customers-gallery'
));


$wp_query = $post_query;
if ($post_query->have_posts()):
    while ($post_query->have_posts()): 
        $post_query->the_post();

        ... output things here ...

    endwhile;

    the_posts_pagination(array(
        'mid_size'           => 5,
        'screen_reader_text' => ' ', 
        'prev_text'          => '<span class="fa fa-angle-left"></span>',
        'next_text'          => '<span class="fa fa-angle-right"></span>'
    ));
endif

wp_reset_postdata();
$wp_query = $original_query;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.