0

I code a page that shows the post results with a couple of filters (the visitor can filter by country and time) and the result is paginated:

$posts_per_page = 10;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query_args = array('posts_per_page' => $posts_per_page, 'paged' => $paged ,  "enable_filter" => array("country", "time"));
$query = WP_Query ($query_args);
$total_post = $query->found_posts;
$pages = $total_post / $posts_per_page;
if ($total_post % $posts_per_page) $pages=$pages+1; 
$args = array(
    'base'               => '%_%',
    'format'             => '?paged=%#%',
    'total'              => $pages,
    'current'            => 0,
    'show_all'           => true,
    'end_size'           => 1,
    'mid_size'           => 2,
    'prev_next'          => true,
    'prev_text'          => __('« Previous'),
    'next_text'          => __('Next »'),
    'type'               => 'plain',
    'add_args'           => false,
    'add_fragment'       => '',
    'before_page_number' => '',
    'after_page_number'  => ''
);
echo paginate_links($args);
?>

The pagination works just fine, the problem is when I apply a filter. When I filter by country and/or time the pagination for the results shows right but when I click on page number 2 I get all the results, meaning that the filters are not applied any longer. How can I preserve the filters during the pagination? Is it done by adding the result to the argument base? If so, is there another option to pass the parameters by post?

Thanks

  • How are you doing the filtering? GET parameters? What is enable_filter? – Jacob Peattie Feb 28 '18 at 16:54
  • @JacobPeattie I am filtering by POST, and enable_filter is a parameter that I send to WP_Query to filter the data. The filter is working but not the pagination when I apply the filters. – Gonzalo Feb 28 '18 at 17:03
  • Well the problem is using a POST request to filter the query. It’s not what POST is for. You would need each link to send a POST request for the next page, but thats not how links or HTTP works. You’re retrieving data, so GET is more appropriate, and in my experience paginate_links() will automatically add the GET parameters to the pagination links. – Jacob Peattie Feb 28 '18 at 17:15
  • @JacobPeattie you were right, I changed to GET and the pagination works automatically. Thanks!!! – Gonzalo Mar 1 '18 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.