WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Session Values Disappears in WordPress (2 posts)

  1. michael
    Member
    Posted 1 year ago #

    I am trying to keep $_POST values in $_SESSION, so I can keep pagination working for a specific search. I have code below:

    if($_POST){
    	foreach($_POST as $key => $value){
    		if($value != ''){
    			$item['taxonomy'] = htmlspecialchars($key);
    			$item['terms'] = htmlspecialchars($value);
    			$item['field'] = 'slug';
    			$list[] = $item;
    		}
    	}
    	$_SESSION['realty-search'] = $list;//!empty($list) ? $list : array();
    }
    
    $search = !empty($_SESSION['realty-search']) ? $_SESSION['realty-search'] : array();
    
    $cleanArray = array_merge(array('relation' => 'AND'), $search);
    $args['post_type'] = 'listings';
    $args['showposts'] = 3;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args['paged'] = $paged;
    $args['tax_query'] = $cleanArray;

    I do a print_r on $_SESSION - and it works when form submits. But as soon as I go in another page - $_SESSION disappears? Is there a specific way I should be handling sessions in Wordrpess?

  2. michael
    Member
    Posted 1 year ago #

    i got it. I forgot session_start().
    thanks though.

Topic Closed

This topic has been closed to new replies.

About this Topic