I have a custom post type of "show" and within that custom post type I have set up a taxonomy / category called "venue" which has two options "venue-one" and "venue-two" (slugs). On the two archive pages I have set up, where each shows all posts from "venue-one" or "venue-two", pagination is not working past page 3. I am using numbered pagination and visually it does display what the correct number of pages should be (given what I set posts per page to) however if you click anything past "3" I am getting a 404.
What I have tried and has not worked:
- resetting permalinks
- changing max number of posts per page directly through wp dashboard
- getting rid of my custom query and using pre_get_posts (note: all my research points to this being the path to fixing my issue, so my assumption is it may work and I am just coding the function incorrectly in functions.php or otherwise). If using pre_get_posts in functions.php is actually the solution, do I need to add anything new in my template files of the problematic archive pages other than the standard wp loop? (and of course getting rid of the custom query).
Just to reiterate, everything works perfectly except for the fact pagination past page 3 gives a 404.
Last note, pagination does work perfectly on the archive page for this custom post as a whole. The issue only occurs on the archive pages for "venue-one" and "venue-two".
WP_Query
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = current_time('Ymd');
$args = array (
'post_type' => 'show',
'posts_per_page' => 3,
'paged' => $paged,
'meta_key' => 'show_date',
'order' => 'ASC',
'orderby' => 'meta_value',
'tax_query' => array(
array(
'taxonomy' => 'venue',
'field' => 'slug',
'terms' => 'venue-one'
)
),
'meta_query' => array(
array(
'key' => 'show_date',
'compare' => '>=',
'value' => $today
),
),
);
$the_query = new WP_Query($args);
?>
Loop
<?php if ($the_query->have_posts() ): while ($the_query->have_posts() ) :$the_query->the_post(); $fields = (object) get_fields(); ?>
Pagination function (in functions.php)
function pagination_bar_venue( $custom_query ) {
$total_pages = $custom_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
));
}
}
Calling pagination in the template
<div class="pagination-shows">
<?php pagination_bar_venue($the_query); ?>
</div>
EDIT + SOME GOOD NEWS Got everything to work w/ the below pre_get_posts function but...the pagination links are not showing up under the posts now. To be clear, the below code has worked and I am now able to see posts beyond page 3 (up until whatever page has posts on it, all working perfect if I type in the url). However once implemented, the links to physically click on the page numbers are gone. How can I pass in $paged variable to the below so it shows back up? or is there another issue?
// get taxonomy posts
function get_tax_posts( $query ) {
// Make sure this only fires when we want it too
if( !is_admin() && $query->is_main_query() && $query->is_tax('venue')) {
// If so, modify the order variables
$query->set('post_type', 'show' );
$query->set('posts_per_page', '3' );
$query->set( 'meta_key', 'show_date' );
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'meta_value' );
$meta_query[] = array(
array(
'key' => 'show_date',
'value' => current_time('Ymd'),
'compare' => '>=',
),
);
$query->set('meta_query',array( $meta_query ) );
$taxquery = array(
array(
'taxonomy' => 'venue',
'field' => 'slug',
'terms' => 'venue-one',
)
);
$query->set( 'tax_query', $taxquery );
}
}
add_action('pre_get_posts', 'get_tax_posts', 9999);
Ok now pagination is fixed but noticed another issue. As for pagination, I swapped out the below:
<?php pagination_bar_venue($the_query);?>
With:
<?php the_posts_pagination();?>
and now pagination works as well.
New issue that is happening is based on the hook priority I believe. As noted above, I have (2) taxonomies to activate this for so I did 2 separate functions in functions php, one for venue-one (code above) and then another for venue-two (with a different function name of course). It seems like you can not have both working. The only one that will work is the one with the higher hook priority number. Is there a fix for this? thanks
EDIT - I think the issue was my function names were too similar, they were not exact but close outside of a few letters. I changed them to be nothing alike at all, and everything seems to be working now!
var_dump($wp_query)
after the query is run, you can see how many pages the main query has in both cases. Usepre_get_posts
to apply your modifications to the main query.