Pagination with $_POST and $_SESSION

I have a page on my site where in the left column is a set of checkboxes representing taxonomies and in the right column is a list of posts that fall into the selected taxonomies. The user is able to check and uncheck these boxes and submit a form which then shows the results in the right column. That works great, here is the simplified code I’m using:

    <?php session_start();

if (isset($_POST)) {
$_SESSION['filters'] = $_POST;
}
?>

<div id="sidebar">
    <form id="filters" method="POST">
        <input type="checkbox" name="filter1" value="term1" />
        <input type="checkbox" name="filter1" value="term2" />
        <input type="checkbox" name="filter1" value="term3" />
        <input type="checkbox" name="filter2" value="term1" />
        <input type="checkbox" name="filter2" value="term2" />
        <input type="checkbox" name="filter2" value="term3" />
</form>
</div>
<div id="results">
    <?php
    //The query is here, which changes dynamically based on the boxes checked above
    I'm using $_SESSION['filters'] to call $_POST variables inside the query
    ?>
</div>

The problem is, when WordPress does its navigation thing, it goes to a URL like mysite.com/thispage/page/2/ which doesn’t retain the form values like I need it to. It resets them as if the form has not been submitted at all. How do I get the selected to values to follow the user when they navigate the results pages.

Read More

This is the first time I’m attempting to use sessions, so I could be on the wrong track altogether. Any help is appreciated.

Related posts

Leave a Reply

1 comment

  1. Add the next lines of code to functions.php file within active theme to start session.

    function example_login() {
    
        if ( ! session_id()) {
            session_start();
        }
    }
    add_action( 'init', 'example_login' );
    

    and then Use $_SESSION to store the value of $post