Pagination not working with pagenavi

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

Topic plugin-wp-pagenavi pagination plugins Wordpress

Category Web


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' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.