Reputation: 2615
If you can figure this one out you're my hero for life!
I'm working on a page that pulls all posts from a custom post type and displays them. At the top of the page is a filter form where users can select a Club and a date. The site will then filter through the content (using a WP_Query) to display what the user has selected. This works great!
However, if you try and paginate this content, something's not quite right! It's probably easier to show you...
will display results for a specific club and date (limited to 2 posts per page), if you click through the pages on the bottom, you'll see that the images don't actually change and that ?paged=2
?paged=3
etc. just get appended to the URL, no pagination logic is taking effect.
The code for the page is the following (pretty long I know)
<?php
$club = $_GET['club'];
$formdate = $_GET['inputdate'];
$date = explode('-', $formdate);
$paged2 = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'sell_media_item',
'collection' => $_GET['club'],
'include_children' => false,
'year' => $date[0],
'monthnum' => $date[1],
'day' => $date[2],
'paged' => $paged2,
'posts_per_page' => 2
); ?>
<?php $loop2 = new WP_Query( $args ); ?>
<?php if ( $loop2->have_posts() ) : while ($loop2->have_posts()) : $loop2->the_post(); ?>
<div class="third gallery-third">
<a href="<?php the_permalink(); ?>"><?php sell_media_item_icon( get_post_meta( $post->ID, '_sell_media_attachment_id', true ) ); ?></a>
</div>
<?php endwhile; ?>
<div class="clearfix"></div>
<?php else: ?>
<p>No images found.</p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div>
<div class="clearfix"></div>
<!-- pagination php -->
<?php
if($loop2->max_num_pages>1){?>
<?php
if ($paged2 > 1) { ?>
<a href="<?php echo '?club=' .$club. '&inputdate='. $formdate . '?paged=' . ($paged2 -1); //prev link ?>" class="page-numbers"><</a>
<?php }
for($a=1;$a<=$loop2->max_num_pages;$a++){?>
<a href="<?php echo '?club=' .$club. '&inputdate='. $formdate . '?paged=' . $a; ?>" <?php echo ($paged2==$a)? 'class="page-numbers selected"':'';?> class="page-numbers"><?php echo $a;?></a>
<?php
}
if($paged2 < $loop2->max_num_pages){?>
<a href="<?php echo '?club=' .$club. '&inputdate='. $formdate . '?paged=' . ($paged2 + 1); //next link ?>" class="page-numbers">></a>
<?php } ?>
<?php } ?>
<!-- end pagination php -->
There is a similar loop above this that states if no filter has been selected, then just get everything. Hence the $loop2
variables.
I think I've covered everything. If you need any more info, feel free to ask away!
Upvotes: 0
Views: 133
Reputation: 2735
Try changing pagination loop to this:
<!-- pagination php -->
<?php
if($loop2->max_num_pages>1){?>
<?php
if ($paged2 > 1) { ?>
<a href="<?php echo '?club=' .$club. '&inputdate='. $formdate . '&paged=' . ($paged2 -1); //prev link ?>" class="page-numbers"><</a>
<?php }
for($a=1;$a<=$loop2->max_num_pages;$a++){?>
<a href="<?php echo '?club=' .$club. '&inputdate='. $formdate . '&paged=' . $a; ?>" <?php echo ($paged2==$a)? 'class="page-numbers selected"':'';?> class="page-numbers"><?php echo $a;?></a>
<?php
}
if($paged2 < $loop2->max_num_pages){?>
<a href="<?php echo '?club=' .$club. '&inputdate='. $formdate . '&paged=' . ($paged2 + 1); //next link ?>" class="page-numbers">></a>
<?php } ?>
<?php } ?>
<!-- end pagination php -->
The only change I did was changing ?paged=
to &paged=
Upvotes: 1