You have two queries that look to me to be competing.
- You are using
query_posts
to alter the main query but are not including any of your filters in that one. - 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. - 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.