Clear or reset the wordpress posts pagination while changing filters

I think it is simple, but I don’t get it.
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>

Related PHP to apply the filter to the wordpress query:

Read More
<?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 do select my “filter” and the result has more than six entries, I use next_posts_link() to see the next six results. The problem is now, if I’m on page 2 or 3 and the other filter has less than e.g. 6 entries, I will see no results while changing my filter.

How do I clear the get variable (/page/2/) while changing my filter?

Example:

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

Now I select “filter” hotels

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

and the “/page/3” will not be cleared. So I can not see some posts.

Related posts

1 comment

  1. It has been anwsered here:
    https://wordpress.stackexchange.com/a/264266

    function get_nopaging_url() {
        $current_url =  $_SERVER[REQUEST_URI];
    
        $pattern = '/page\/[0-9]+\//i';
        $nopaging_url = preg_replace($pattern, '', $current_url);
    
        return  $nopaging_url;
    }
    

    You could use this function to remove the pagination in your urls with filters.

    See example:

    <a href="<?php echo get_nopaging_url(); ?>?filter=houses">
    

Comments are closed.