Clear or reset wordpress places pagination when changing filters

I think itโ€™s easy, but I donโ€™t understand. This is my filter:

<form class='post-filters'>
    <select name="filter">
        <?php
        $filter_options = array(
            'houses' => 'Houses',
            'hotels' => 'Hotels',
        );
        foreach( $filter_options as $value => $label ) {
            echo "<option ".selected( $_GET['filter'], $value )." value='$value'>$label</option>";
        }
        ?>
    </select>
    <input type='submit' value='Filter!'>
</form>

PHP related to apply filter to wordpress request:

<?php 
    global $destinations;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $destinations = new WP_Query([
        'paged' => $paged,
        'location' => $location,
        'category_name' => urldecode(get_query_var('filter')),
        'posts_per_page' => 6
    ]);
 ?>

If I select my โ€œfilterโ€ and the result has more than six entries, I use next_posts_link()to view the next six results. The problem now, if I am on page 2 or 3, and the other filter is less than, for example, 6 records, I do not see any results when the filter is changed.

How to clear get (/ page / 2 /) variable when filter changes?

Example:

category/subcategory/subsubcategory/page/3/?filter=houses 

Now I select the filter hotels.

category/subcategory/subsubcategory/page/3/?filter=hotels

and "/ page / 3" will not be deleted. Therefore, I do not see some messages.

+4
source share

Source: https://habr.com/ru/post/1598289/


All Articles