Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
indeed! just got it myself but will mark your answer as correct. thanks! –  dalbaeb Nov 21 '12 at 19:41

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">
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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