0

I am using PageNavi and have been searching around trying to figure out why I can't get pagination to work. Currently, the page numbers show but when you click on the page numbers, it goes to a page not found page.

Here are some links I've reviewed:

https://toolset.com/forums/topic/help-to-get-wp-pagenavi-working-for-page-2-onwards/

pagenavigation not showing the next page just linking back to the main page

Here is my code:

<?php
    /*Query DW Hammer Articles*/
    $counter = 1; //counter tracks the number of the post we're on
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
        'posts_per_page' => 2,
        'post_type' => 'news_article',
        'orderby'=> 'date',
        'order' => 'DESC',
        'paged' => $paged
    );
    $newsQuery = new WP_Query( $args);
    if ( $newsQuery->have_posts()) : while ( $newsQuery->have_posts()) : $newsQuery->the_post();
        ?>
        <?php get_template_part('templates/content', get_post_type() != 'post' ? get_post_type() : get_post_format()); ?>
        <?php $counter++; endwhile; ?>
        <?php wp_reset_query(); endif;?>
    <div class="wp-pagenavi-container">
        <?php wp_pagenavi( array( 'query' => $newsQuery ) ); wp_reset_query();?>
    </div>
  • And where do you put this code? – Krzysiek Dróżdż May 24 '18 at 19:45
  • @KrzysiekDróżdż This code is on the news archive listing page. – proteus-tstokes May 24 '18 at 19:47
  • Then it won’t work. You ignore real query and put your own. So the real query can have less results than your query and will generate 404. You should use pre_get_posts and not own WP_Query in such case. – Krzysiek Dróżdż May 24 '18 at 19:49
  • @KrzysiekDróżdż Can you please provide what I should do then because every where I've researched shows to do it similar to how I have it. – proteus-tstokes May 24 '18 at 19:54
0

I added the following to my functions.php file and it worked:

function toolset_fix_custom_posts_per_page( $query_string ){
if( is_admin() || ! is_array( $query_string ) )
    return $query_string;

$post_types_to_fix = array(
    array(
        'post_type' => 'news_article',
        'posts_per_page' => 6
    ),
    // add another if you want
    /*
    array(
        'post_type' => 'movie',
        'posts_per_page' => 2
    ),
    */
);

foreach( $post_types_to_fix as $fix ) {
    if( array_key_exists( 'post_type', $query_string )
        && $query_string['post_type'] == $fix['post_type']
    ) {
        $query_string['posts_per_page'] = $fix['posts_per_page'];
        return $query_string;
    }
}

return $query_string;

}

add_filter( 'request', 'toolset_fix_custom_posts_per_page' );

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.