0

I have a list of events that are displaying in date order, with events in the past being hidden.

The issue is that in total, there are 3 pages of events, but once the past events are hidden, there are only enough events to fill 2 pages. However, a third, blank page is still present and can be accessed through the paging links.

This is the code on my events list page:

<?php $today = date("Ymd");?>

<?php $paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=upcomingevents&paged=' . $paged );?> 

<?php $loop = new WP_Query( array( 'post_type' => 'upcomingevents', 'paged'=> $paged, 'meta_key' => 'start_date', 'meta_compare' => '>=', 'meta_value' => $today, 'orderby' => 'meta_value_num', 'order' => 'ASC' )); ?>
<?php while ( $loop->have_posts('post_type=upcomingevents') ) : $loop->the_post(); ?>

And this is the paging code in my functions file:

function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
    'base' => @add_query_arg('page','%#%'),
    'format' => '',
    'total' => $wp_query->max_num_pages,
    'current' => $current,
    'show_all' => true,
    'type' => 'plain'
);
if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );

}

0

You have two queries that look to me to be competing.

  1. You are using query_posts to alter the main query but are not including any of your filters in that one.
  2. You are creating a new query with your filters-- 'meta_key' => 'start_date', etc-- and looping over that query result set to show your posts.
  3. Then to paginate you using the main query-- $wp_query-- which doesn't have any of your filtering. It isn't surprising that things are out of sync.

You need to re-factor this to only use one query and not jumble the two together. I believe you can use the $loop object just as you are using $wp_query in the pagination function.

function paginate($loop) {
  global $wp_rewrite;
  $loop->query_vars['paged'] > 1 ? $current = $loop->query_vars['paged'] :   $current = 1;
  $pagination = array(
    'base' => @add_query_arg('page','%#%'),
    'format' => '',
    'total' => $loop->max_num_pages,
    'current' => $current,
    'show_all' => true,
    'type' => 'plain'
  );
  // ...

I can't promise that that is a complete solution. What you have got is complicated and difficult to test without duplicating your database and page structure.

It is also possible that you could use a pre_get_posts filter to alter the main query and save the database some pain.

| improve this answer | |
  • Hi there, Thanks for your response. I tried those changes in the functions file you suggested, but the paging just disappeared all together. The query code I have copied and pasted from multiple places (as you can probably tell) to get it to do everything I need to do. Is there a way I can put all of my code into a single query that anyone can suggest - I'm not a developer so I can't write this stuff myself. Thank you! – rachel Apr 17 '13 at 15:54
  • hmmm... I've used paginate_links very similarly. var_dump($pagination); for me. Edit the question please. – s_ha_dum Apr 17 '13 at 20:23
  • Sorry I don't understand - What do i need to edit in the question? – rachel Apr 18 '13 at 8:31
  • I want to know what your $pagination array looks like. So, add var_dump($pagination); after you create that array and paste the output into the question. – s_ha_dum Apr 18 '13 at 13:04
  • Ok, sorry about that. thanks for explaining! This is what printed: array(6) { ["base"]=> string(40) "http://wordpresstesting/events/page/%#%/" ["format"]=> string(0) "" ["total"]=> float(3) ["current"]=> int(1) ["show_all"]=> bool(true) ["type"]=> string(5) "plain" } – rachel Apr 18 '13 at 16:18

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.