0

I am trying to make a custom page template that shows the most viewed posts. I can return posts but I am having trouble figuring out how to paginate it. Here is what I have:

$args = array('orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();

I tried:

$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 );
            while ( $loop->have_posts() ) : $loop->the_post();

but my pagination still doesn't show up. I am wondering if it has to do with my pagination function or the way I am setting up my query.

I just call my pagination function below the query, it all looks like this:

<?php
            $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 );
            while ( $loop->have_posts() ) : $loop->the_post();?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>

<?php pagination(); ?>

This is my pagination function:

if ( ! function_exists( 'pagination' ) ) :
    function pagination() {
        global $wp_query;

        $big = 999999999; // need an unlikely integer

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

Any help would be greatly appreciated.

  • Looks like you are doing a local query, where the pagination is using global query variable. Replace this line - $loop = new WP_Query( $args ); to query_posts( $args ). Also replace - $loop->have_posts() ) : $loop->the_post() to have_posts() ) : the_post(). And you don't need to use wp_reset_postdata(). – Shazzad Jul 12 '14 at 17:35
  • I've heard you shouldn't use query_posts to output a custom loop. – justinw Jul 12 '14 at 17:39
  • That's best practice. Using global pagination ( get_query_var('paged') ) into a custom query isn't also a best practice. However, i am writing an answer in few seconds. – Shazzad Jul 12 '14 at 17:45
  • I found a post on this. Using that method works quite well. – justinw Jul 12 '14 at 17:46
2

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

So, change the pagination function to take 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 )
            $max_page = $wp_query->max_num_pages;

        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();
0

Try this..

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

$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(); // Pagination Function

endif;
wp_reset_postdata();

The pagination should be added before the wp_reset_postdata();.

  • Unfortunately, it didn't paginate. – justinw Jul 12 '14 at 17:21

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.