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.
$loop = new WP_Query( $args );
toquery_posts( $args )
. Also replace -$loop->have_posts() ) : $loop->the_post()
tohave_posts() ) : the_post()
. And you don't need to usewp_reset_postdata()
.query_posts
to output a custom loop.