Pagination : How to remove /page/x/ after a ‘POST’ action on a form returning to page 1

I have the following query, what it does is get a list of “Books” (custom post type). This list is displayed on a page which also has a small form with a dropdown populated with the terms of “Editors” (custom taxonomy). When a user chooses an “Editor”, it reloads the page and the query returns the corresponding “Books”.

// Taxonomy query
$tax_query = array('relation' => 'AND');
if($_POST['editor']) array_push($tax_query,array('taxonomy' => 't_editor','terms' => array($_POST['editor']),'field' => 'slug'));

// Pagination
if($_POST['reset_pagination'] || !get_query_var('paged')){
    $paged = 1;
}else if (get_query_var('paged')) {
    $paged = get_query_var('paged');
}

// Query
$myquery = array(
    'post_status' => 'publish',
    'post_type' => 'books',
    'orderby' => 'title',
    'order' => 'ASC',
    'tax_query' => $tax_query,
    'paged' => $paged
);

As there are many many “Books”, I use pagination.

Read More

Now, the problem. Lets say I’m on the general list (non-filtered by “Editor”), I have 200 results, with a pagination of ten per page. Let’s say I browse until page 8. Then, if I decide to filter my choice by “Editor”, the page reloads and displays only those results. The problem is that as I was on page 8, my URL has /page/8/ attached to it.

I found how to reset “paged” to 1 after a filtering has been made (with a hidden “reset_pagination” field on the form), it works and I have the results of page 1 after a filtering, BUT in the URL the pagination (/page/8/) remains.

How can I remove “/page/x/” from the URL ?

Edit : Here’s the code for the form

<form action="" method="post" id="filter">
<ol>
    <li>
        <?php include(TEMPLATEPATH . '/includes/forms/filter_editors.php'); ?>
    </li>
    <li>
        <input type="hidden" name="reset_pagination" value="1"/>
        <input type="submit" class="submit" value="<?php _e('Ok'); ?>" />
    </li>
</ol>

Related posts

Leave a Reply

1 comment

  1. Your form will post back to the URL specified in the action parameter, or to the same URL if you don’t specify it. You start at /books/page/8/, so if your action parameter is empty, the resulting URL will also be /books/page/8/. Just fill in the action parameter with /books/, and it should work.