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 form on a page template that controls which posts show up on the page and how those posts are ordered. You can see what I mean here: http://www.theseattlevine.com/the-vine

The questions I have is, how do I get the form to "remember" the selected values when the user uses the pagination at the bottom to go to the next page or a specific page?

EDIT: Looks like sessions in the answer, but I can't get it to work. Any help is appreciated.

Here is my current code after formatting and adding the suggestion below. It's still not working.

    <?php
 /**
 * Template Name: The Vine
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */ ?>

<?php
    //Form Processing-----------------------------------------------------------------
    //Display chosen number of posts per page
    $gridperpage = array("8", "16", "32", "64", "96");
    $listperpage = array("10", "20", "40", "65", "100");
    $perpagecurrent = $_POST['perpage'];
    //If the number is already set, use it

    if (isset($_POST['perpage'])) {
        $perpage = $_POST['perpage'];
    }

    //If the number of posts matches the multiples of the grid numbers AND we're on the list page, switch the count to 20

    if ((!in_array($perpagecurrent, $listperpage)) && (($_POST['view']) == 'list') ){
        $perpage = '20';
    }

    //If we're on the grid page and the number is a multiple from the list page, switch to 32. Also do this if there is no value set yet since the default view is the grid
    elseif (((!in_array($perpagecurrent, $gridperpage)) && (($_POST['view']) == 'grid')) || (!$_POST['perpage']) ){
        $perpage = '32';
    }

    //Display chosen post types

    if ((!$_POST['userposts']) && (!$_POST['posts'])) :
    $posttype= array('tsv_userpost', 'post');
    elseif ((($_POST['userposts']) == 'true') && (($_POST['posts']) == 'true')) :
    $posttype= array('tsv_userpost', 'post');
    elseif ((($_POST['userposts']) == 'true') && (($_POST['posts']) == '')) :
    $posttype= 'tsv_userpost';
    elseif ((($_POST['userposts']) == '') && (($_POST['posts']) == 'true')) :
    $posttype= 'post';
    endif;
    //Display only selected User Post Categories

    if (isset($_POST['showvideos']) && ($_POST['showvideos'] == 'true')) {
        $cats[]='user-video';
    }


    if (isset($_POST['showimages']) && ($_POST['showimages'] == 'true')) {
        $cats[]='user-image';
    }


    if (isset($_POST['showaudio']) && ($_POST['showaudio'] == 'true')) {
        $cats[]='user-audio';
    }


    if (isset($_POST['showwriting']) && ($_POST['showwriting'] == 'true')) {
        $cats[]='user-writing';
    }


    if (isset($_POST['showevents']) && ($_POST['showevents'] == 'true')) {
        $cats[]='user-event';
    }

    ;
    $upostcat = implode(',',$cats); //Put all the selected categories into the $upostcat array
    ?> 
<?php 
    //Sort posts by selected method

    if (isset($_POST['sortby']) && ($_POST['sortby'] == 'date')) {
        $sortmetakey='';
        $sortby='date';
    }

    elseif (isset($_POST['sortby']) && ($_POST['sortby'] == 'rating')) {
        $sortmetakey='ratings_average';
        $sortby='meta_value_num date';
    }

    elseif (isset($_POST['sortby']) && ($_POST['sortby'] == 'favscount')) {
        $sortmetakey='wpfp_favorites';
        $sortby='meta_value_num date';
    }

    elseif (isset($_POST['sortby']) && ($_POST['sortby'] == 'viewcount')) {
        $sortmetakey='_count-views_all';
        $sortby='meta_value_num date';
    }

    ;
    ?>

<?php //Session

    session_start();
    // your formfields
    $post_fields = array( 'sortby', 'view', 'perpage' , 'posts' , 'userposts' , 'showaudio' , 'showvideos' , 'showimages' , 'showevents' , 'showwriting' );
    $form_data = array();
    // copy needed form data from $_POST array
    foreach ( $post_fields as $key )
    if ( isset( $_POST[$key] ) )    $form_data[$key] = $_POST[$key];
    // save your form data in a session if no form data was saved before

    if (  ! empty( $form_data )    &&  ! isset( $_SESSION['form_data'] ))  $_SESSION['form_data'] = serialize( $form_data );
    // read saved form data from session

    if (  isset( $_SESSION['form_data'] )    &&  ! empty( $_SESSION['form_data'] )    &&  empty( $form_data ))  $form_data = unserialize( $_SESSION['form_data'] );
    ?> 



<?php 
    //Start of content
    get_header();
    ?>
<div id="vine-menu">
<?php  //Search the vine ?>
<div class="vine-search">
    <form method="get" id="searchform" action="<?php  echo esc_url( home_url( '/' ) ); ?>">
        <label for="s" class="assistive-text"><?php  _e( 'Search', 'twentyeleven' ); ?></label>
        <input type="text" class="field" name="s" id="vinesearch" placeholder="<?php  esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
        <input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php  esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
    </form>
</div>
<?php  //Browse the vine ?>
<?php
 //Start filter menu
 ?>
    <form method="post" id="vine-sort-form" >
    <?php   

    if ($_SERVER['REQUEST_METHOD']!='POST') {
        $notsubmitted='checked';
        $notsubmitted_dd='selected';
    }

    ;
    //Auto-select all post types when user first selects to show userposts  

    if ('true' == ($_POST['userposts']) && ('' == ($_POST['showvideos'])) && ('' == ($_POST['showaudio'])) && ('' == ($_POST['showimages'])) && ('' == ($_POST['showwriting'])) && ('' == ($_POST['showevents'])) ) {
        $userpostselected='checked';
    }

    ;
    //Dont allow both post types to go unselected

    if ('true' != ($_POST['userposts']) && ('true' != ($_POST['posts']))) {
        $noselectnone='checked';
    }

    ;
    ?>
    <?php php//Choose View  ?>
    <?php
 if (($_POST['view'] == 'grid') || (!isset($_POST['view']))) { ?>
    <input id="viewgrid" type="radio" name="view" value="grid" onclick="this.form.submit()" <?php 

        if ('grid' == ($_POST['view'])) :
        echo 'checked';
        elseif ($notsubmitted) :
        echo $notsubmitted;
        endif ?> style="display:none;">
    <label class="imagelabel" id="gridchecked" for="viewgrid"><img src="/wordpress/image/icons/grid-on.png" /></label>
    <?php  } else { ?>  
    <input id="viewgrid" type="radio" name="view" value="grid" onclick="this.form.submit()" <?php 

        if ('grid' == ($_POST['view'])) :
        echo 'checked';
        elseif ($notsubmitted) :
        echo $notsubmitted;
        endif ?> style="display:none;">
    <label class="imagelabel" id="gridunchecked" for="viewgrid"><img src="/wordpress/image/icons/grid-off.png" /></label>
    <?php  } ?>
    <?php
 if ($_POST['view'] == 'list') { ?>
    <input id="viewlist" type="radio" name="view" value="list" onclick="this.form.submit()" <?php 

        if ('list' == ($_POST['view'])) :
        echo 'checked';
        endif ?> style="display:none;">
    <label id="listchecked" class="imagelabel" for="viewlist"><img src="/wordpress/image/icons/list-on.png" /></label>
    <?php  } else { ?>
    <input id="viewlist" type="radio" name="view" value="list" onclick="this.form.submit()" <?php 

        if ('list' == ($_POST['view'])) :
        echo 'checked';
        endif ?> style="display:none;">
    <label id="listunchecked" class="imagelabel" for="viewlist"><img src="/wordpress/image/icons/list-off.png" /></label>
    <?php  } ?>
    <br />
    <div class="vine-menu-heading">Per page:</div>
    <?php  if ((($_POST['view']) == 'grid') || (!isset($_POST['perpage']))) { ?>
    <select name="perpage" id="perpage">
        <option value="8" <?php 

        if ('8' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">2 rows (8)</option>
        <option value="16" <?php 

        if ('16' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">4 rows (16)</option>
        <option value="32" <?php 

        if ('32' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        elseif ((in_array($perpagecurrent, $listperpage))) :
        echo 'selected="selected"';
        elseif (!$_POST['perpage']) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">8 rows (32)</option>
        <option value="64" <?php 

        if ('64' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">16 rows (64)</option>
        <option value="96" <?php 

        if ('96' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">24 rows (96)</option>
    </select>
    <?php  } else { ?>
    <select name="perpage" id="perpage">
        <option value="10" <?php 

        if ('10' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">10 posts</option>
        <option value="20" <?php 

        if ('20' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        elseif ((in_array($perpagecurrent, $gridperpage))) :
        echo 'selected="selected"';
        elseif (!$_POST['perpage']) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">20 posts</option>
        <option value="40" <?php 

        if ('40' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">40 posts</option>
        <option value="65" <?php 

        if ('65' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">65 posts</option>
        <option value="100" <?php 

        if ('100' == ($_POST['perpage'])) :
        echo 'selected="selected"';
        endif;
        ?> onclick="this.form.submit()">100 posts</option>
    </select>
    <?php  } ?>
    <div class="vine-menu-heading">Sort by:</div>
    <input type="radio" name="sortby" value="date" onclick="this.form.submit()" <?php 

    if ('date' == ($_POST['sortby'])) :
    echo 'checked';
    elseif ($notsubmitted) :
    echo $notsubmitted;
    endif;
    ?>>Date<br />
    <input type="radio" name="sortby" value="rating" onclick="this.form.submit()" <?php 

    if ('rating' == ($_POST['sortby'])) :
    echo 'checked';
    endif ?>>Rating<br />
    <input type="radio" name="sortby" value="favscount" onclick="this.form.submit()" <?php 

    if ('favscount' == ($_POST['sortby'])) :
    echo 'checked';
    endif ?>># of Favs<br />
    <input type="radio" name="sortby" value="viewcount" onclick="this.form.submit()" <?php 

    if ('viewcount' == ($_POST['sortby'])) :
    echo 'checked';
    endif ?>># of Views<br />
    <div class="vine-menu-heading">Post Type:</div>
    <input type="checkbox" name="posts" value="true" onclick="this.form.submit()" <?php 

    if ('true' == ($_POST['posts'])) :
    echo 'checked';
    elseif ($notsubmitted) :
    echo $notsubmitted;
    elseif($noselectnone) :
    echo $noselectnone;
    endif;
    ?>>Featured Posts<br />
    <input type="checkbox" name="userposts" value="true" onclick="this.form.submit()" <?php 

    if ('true' == ($_POST['userposts'])) :
    echo 'checked';
    elseif ($notsubmitted) :
    echo $notsubmitted;
    elseif($noselectnone) :
    echo $noselectnone;
    endif;
    ?>>User Posts<br />
        <div class="user-cat-select">
        <?php 

    if ('true' == ($_POST['userposts'])) {

        if ('' == ($_POST['posts'])) {
            ?>
        <input type="checkbox" name="showvideos" value="true" onclick="this.form.submit()" <?php 

            if ('true' == ($_POST['showvideos'])) :
            echo 'checked';
            elseif ($notsubmitted) :
            echo $notsubmitted;
            elseif ($userpostselected) :
            echo $userpostselected;
            endif;
            ?>>Video<br />
        <input type="checkbox" name="showimages" value="true" onclick="this.form.submit()" <?php 

            if ('true' == ($_POST['showimages'])) :
            echo 'checked';
            elseif ($notsubmitted) :
            echo $notsubmitted;
            elseif ($userpostselected) :
            echo $userpostselected;
            endif;
            ?>>Images<br />
        <input type="checkbox" name="showwriting" value="true" onclick="this.form.submit()" <?php 

            if ('true' == ($_POST['showwriting'])) :
            echo 'checked';
            elseif ($notsubmitted) :
            echo $notsubmitted;
            elseif ($userpostselected) :
            echo $userpostselected;
            endif;
            ?>>Writing<br />
        <input type="checkbox" name="showaudio" value="true" onclick="this.form.submit()" <?php 

            if ('true' == ($_POST['showaudio'])) :
            echo 'checked';
            elseif ($notsubmitted) :
            echo $notsubmitted;
            elseif ($userpostselected) :
            echo $userpostselected;
            endif;
            ?>>Audio<br />
        <input type="checkbox" name="showevents" value="true" onclick="this.form.submit()" <?php 

            if ('true' == ($_POST['showevents'])) :
            echo 'checked';
            elseif ($notsubmitted) :
            echo $notsubmitted;
            elseif ($userpostselected) :
            echo $userpostselected;
            endif;
            ?>>Events<br />
        <?php 
        }

    }

    ?>
        </div>      
    </form>
</div><?php  //.thevinemenu ?>



<div class="vine-display"><?php //.vine-display ?>
        <?php
    // The Query  
    $counter = 1;
    $paged = (get_query_var('paged')) ? get_query_var('paged') :
    1;
    $the_query = new WP_Query( array('post_type'=> $posttype,'posts_per_page'=>$perpage,'paged'=> $paged,'category_name'=>$upostcat,'meta_key' =>$sortmetakey, 'orderby' =>$sortby, 'order'=> 'DESC'));
    //Top Navigation
    wp_pagenavi( array( 'query' => $the_query ) );
    //Start loop
    while ( $the_query->have_posts() ) :
    $the_query->the_post();

    if ('grid' == ($_POST['view']) || ($_SERVER['REQUEST_METHOD']!='POST')) :
    get_template_part('the-vine', 'grid');
    elseif ('list' == ($_POST['view'])) :
    get_template_part('the-vine', 'list');
    endif;
    ?>  
<?php  endwhile; ?>
<div class="wp-page-navi-bottom"><?php  wp_pagenavi( array( 'query' => $the_query ) ); ?></div>
<?php  wp_reset_query(); ?>
</div>
<?php  get_footer(); ?>
share|improve this question
    
I've updated the code since I'm moved further along with this project. About half of the form fields work now so you can see the functionality. Hopefully someone will have an idea of how I can achieve this. I'm really stuck. –  Eckstein Dec 9 '12 at 23:11
    
Code updated again. The form now works, it's just the pagination I'm looking for figure out. –  Eckstein Dec 24 '12 at 21:28
add comment

1 Answer

up vote 2 down vote accepted

Use sessions

<?php
session_start();

 // your fomrfields
$post_fields = array( 'key_one', 'key_two', 'key_three' );

$form_data = array();

// copy needed form data from $_POST array
foreach ( $post_fields as $key )
  if ( isset( $_POST[$key] )
    $form_data[$key] = $_POST[$key];

// save your form data in a session if no form data was saved before
if (
  ! empty( $form_data )
    &&
  ! isset( $_SESSION['form_data'] )
)
  $_SESSION['form_data'] = serialize( $form_data );

// read saved form data from session
if (
  isset( $_SESSION['form_data'] )
    &&
  ! empty( $_SESSION['form_data'] )
    &&
  empty( $form_data )
)
  $form_data = unserialize( $_SESSION['form_data'] );

From now on use $form_data[key] instead of $_POST[key]

share|improve this answer
    
Thank you for your help! I'm new to using sessions so I'm going to try implementing this over the next few hours and hopefully I can work it out. –  Eckstein Dec 10 '12 at 0:01
    
I tried implementing this but it's not working out for me. I've updated the code above to what I have now, but the form fields still aren't saving when I use the pagination. –  Eckstein Dec 21 '12 at 4:10
    
Debugging starts with code formatting. Use indents and new lines. This make your code longer but you find errors much faster. I wasted years of my life to find errors in bad formatted code until I format my code well. You have an error in your braces at // read saved form data from session. I reformat the code above a bit, do the same with your (complete) code and you will find the error. –  Ralf912 Dec 22 '12 at 21:28
    
When you say "From now on use $form_data[key] instead of $_POST[key]" do mean I should replace all existing instances of $_POST[key] that I currently have? Also, there is a typo in your example. Instead of "if ( isset( $_POST[$key] )" it should be "if ( isset( $_POST[$key] ) )". There is a missing parenthesis. –  Eckstein Dec 24 '12 at 21:25
add comment

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.