Pagination custom query

This is a local/global variable problem. It can be solved either by changing the pagination function to work with local variable, or by promoting the local variable into global scope. As you are using wp_reset_postdata(), i guess you want to keep the original query.

Change the pagination function to accept arguments –

if ( ! function_exists( 'pagination' ) ) :

    function pagination( $paged = '', $max_page="" ) {
        $big = 999999999; // need an unlikely integer
        if( ! $paged ) {
            $paged = get_query_var('paged');
        }

        if( ! $max_page ) {
            global $wp_query;
            $max_page = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
        }

        echo paginate_links( array(
            'base'       => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format'     => '?paged=%#%',
            'current'    => max( 1, $paged ),
            'total'      => $max_page,
            'mid_size'   => 1,
            'prev_text'  => __( '«' ),
            'next_text'  => __( '»' ),
            'type'       => 'list'
        ) );
    }
endif;

And the loop template will be like –

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; 
$args = array(
    'paged'          => $paged, 
    'orderby'        => 'meta_value_num', 
    'meta_key'       => 'post_views_count', 
    'posts_per_page' => 36 
);
$loop = new WP_Query( $args );

if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : 
        $loop->the_post();
        get_template_part( 'content', get_post_format() );
    endwhile;

    pagination( $paged, $loop->max_num_pages); // Pagination Function
endif;

wp_reset_postdata();

Leave a Comment