WordPress pagination not working in search result page

I have a problem with pagination on search results page. First time it working after I click on submit button in search form.

The problem when I click on pagination, $_POST get null.

Read More

Thanks for help

<?php
foreach($_POST as $key => $value){
   if($value != ''){
     $item['taxonomy'] = htmlspecialchars($key);
     $item['terms'] = htmlspecialchars($value);
     $item['field'] = 'term_id';
     $list[] = $item;
   }        
}  
$cleanArray = array_merge(array('relation' => 'AND'), $list);
$args['post_type'] = 'listings';
$args['showposts'] = 12;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;  
$args['tax_query'] = $cleanArray; 
$the_query = new WP_Query( $args ); //data is sanitized inside wp_query class  

while ( $the_query->have_posts() ) : $the_query->the_post();

the_title();
endwhile;
?>

Related posts

Leave a Reply

1 comment

  1. Try this code

    <?php
    $list = array();
    $item = array();
    if(!empty($_POST)) {
           $_SESSION['your-var'] = $_POST;
    }
    
      foreach($_SESSION['your-var'] as $key => $value){
       if($value != ''){
         $item['taxonomy'] = htmlspecialchars($key);
         $item['terms'] = htmlspecialchars($value);
         $item['field'] = 'term_id';
         $list[] = $item;
       }        
    }  
    $cleanArray = array_merge(array('relation' => 'AND'), $list);
    $args['post_type'] = 'listings';
    $args['showposts'] = 12;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args['paged'] = $paged;  
    $args['tax_query'] = $cleanArray; 
    $the_query = new WP_Query( $args ); //data is sanitized inside wp_query class  
    
    while ( $the_query->have_posts() ) : $the_query->the_post();
    
    the_title();
    endwhile;
    ?>