wp paginate_links and query vars not working Wordp

2019-09-05 04:13发布

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?

1条回答
姐就是有狂的资本
2楼-- · 2019-09-05 04:57

You dont need to use any plugin for sorting below Im posting you a simple code using isset

<?php 
$orderby = 'date';
$order = isset( $_REQUEST['sort'] ) ? trim( $_REQUEST['sort'] ) : null;
if(isset($_GET['sort']) && !empty($_GET['sort'])) {
    switch($order)
    {
        case 'views' :  $meta_key = '_kksr_avg';
                                $orderby = 'meta_value_num';
            break;

        case 'newest' : $meta_key = 'post_views_count'; 
                                  $orderby = 'date';
            break;
    }
}
$args = array(
'posts_per_page' => 20,
'meta_key' => $meta_key,
'orderby' => $orderby,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), );
query_posts($args);  ?> 
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>

 // code for display posts

<?php 

endif; 

endwhile; 

else : 

endif;

custom_pagination($query->max_num_pages,"",$paged);

wp_reset_query();

?>
查看更多
登录 后发表回答