I am trying to make custom query integrated with a plugin to sort posts.
Here's the code.
index.php
$view =$_GET["sort"];
if($view == "views"){
if ( have_posts() ) :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post', 'meta_key' => '_kksr_avg', 'orderby' => 'meta_value', 'order' => 'desc', 'paged' => $paged, 'posts_per_page' => 5);
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
// code for display posts
endwhile;
if (function_exists(custom_pagination)) :
custom_pagination($query->max_num_pages,"",$paged);
wp_reset_postdata();
endif;
endif;
}
And here is the functions.php code.
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}
The problem comes with the paginate links. conflict with the query string.
domain.com/?sort=views -> for first page
domain.com/?sort=viewspage/2 ->for sec. page
This is not working.
Is there any solution to fix this?