0

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!

10
  • Whether or not an archive page exists is dependent on the contents of the main query, anything you do in the template is completely irrelevant. WordPress decided it was a 404 before the template ever gets loaded, this is how it's able to load a 404.php template in the case of a 404. The only solution is to modify the main query before it is run.
    – Milo
    Commented Jan 23, 2019 at 16:29
  • any chance you can advise on how to do this? given the query I am running? In other words, how exactly should I modify the main query before its run to accomplish my objective? I do not at all doubt your answer but it seems strange this issue does not exist on archive pages for CPT, only taxonomy pages.
    – James
    Commented Jan 23, 2019 at 16:41
  • For whatever reason your custom taxonomy queries results in fewer pages and your main archive query does not. If you var_dump($wp_query) after the query is run, you can see how many pages the main query has in both cases. Use pre_get_posts to apply your modifications to the main query.
    – Milo
    Commented Jan 23, 2019 at 17:03
  • Appreciate your help. Sorry I am new here, do you suggest I create a new post to figure out exactly how to code the pre_get_posts function in this scenario? I saw your solution for another user who had the same exact issue and I used the code you provided to that person however it did not work for me. I spent hours researching and using different ways of implementing the pre_get_posts function with no luck. I assume I am coding it incorrectly.
    – James
    Commented Jan 23, 2019 at 17:08
  • Also, I thought this was very interesting - when I var_dump the max number of pages - it returns 5! Yet the posts simply do not show up on pages 4-5. You would think, it would return 3?
    – James
    Commented Jan 23, 2019 at 19:11

1 Answer 1

0

I had a similar issue and maybe this is also helpful when pagination is only working sometimes: e.g. /page/1/ and /page/2/ are working and /page/3/ not. Result: Error 404. Problem is: default value (12 items each page) for posts_per_page (WordPress Settings/Reading) is loaded always from database before the template is loaded. So $args=['posts_per_page' => 1] in custom_post WP_Query($args) has effect on pagination and number of loaded elements but not on paged rewrite Rule! Proposed solution is to add pre_get_posts() to function.php and separate by custom post_type. Example:

//Add to functions.php because it have to be loaded before template loading
function hwl_home_pagesize( $query ) {
    if ( get_post_type() == "your_custom_post_type" )
        $query->query_vars['posts_per_page'] = 2; //same as in WP_Query arg or add to WP_Query arg get_option('posts_per_page')
}
add_action('pre_get_posts', 'hwl_home_pagesize', 1);

see also: https://core.trac.wordpress.org/ticket/22299 (From my point of view, it is still an architecture issue, no matter what was already said 9years ago.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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