0

I have a custom template with a drop-down that filters posts by category. I do it by making the form post to the page itself and then catching the argument and using it in a query (see code below).

Here's the bummer: if you navigate to page 2 (for example) and then use the filter, the URL still contains the /page/2/ bit, and you can sometimes end up with zero posts visible if there are less than a page's worth of results, since the new results all on page 1 and you're stuck on page 2. Tried unsetting the paged variable, to no avail. How can I send the user back to first page every time they use the filter?

<form action="" method="POST">
    <fieldset>
        <select id="food-type" name="food_type" class="selectBox">
            <option value="0">Select One:</option>
            <option value="meat">Meat</option>
            <option value="salads">Salads</option>
            <option value="vegetarian-entrees">Vegetarian Entrees</option>
        </select>
        <button type="submit" class="search">Search</button>
    </fieldset>
</form>

I process its contents as follows:

global $post;
global $query_string;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'post_status'      => 'publish',
    'post_type'        => 'recipe',
    'suppress_filters' => false,
    'paged'   => $paged
);

if(isset($_POST['select_food_type']) && !empty($_POST['select_food_type']) ) {
    $args['food_type'] = $_POST['select_food_type'];
    unset($args['paged']);
}
query_posts($args);

2 Answers 2

0

Perhaps set your form's action to point to the first page of paged results, rather than the default (empty) of the current page.

<form action="_URL_to_first_page_" method="POST">

Keep in mind the URL is determined on the client side in the case of an empty action.

1
  • indeed! just got it myself but will mark your answer as correct. thanks! Commented Nov 21, 2012 at 19:41
0

Here's the solution. Make sure the form posts to the proper page every time instead of posting to the current URL.

<form action="<?php echo get_permalink(); ?>" method="POST">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.