Good morning. I created a search filter with ajax and it is having problems with the pagination that is created with the result of the filtering. When the page displays filtered pagination and the user changes pages (Goes to page 2, for example), the page loads by marking page one with the results of the first search performed. Example: searched for the word "arvore", returned 100 results divided into 10 pages pages. Filtered for the last month and returned 70 results divided into 7 pagination pages. When you switch to page 2, instead of marking you on page 2 of the 70 results, it marks you are on page 1 of the 100 initial results. However, it shows what a second page of the results would look like.
function load_search_results(){
global $wp_query;
$order = 'DESC';
$dateAfter = date('F jS, Y', 1970) ;
if(isset($_POST['query'])){
$query = sanitize_text_field($_POST['query']);
}
if(isset($_POST['index'])){
$index = sanitize_text_field($_POST['index']);
if($index == 1 ){
$order = 'ASC';
}
}
if(isset($_POST['radio'])){
$radio = sanitize_text_field($_POST['radio']);
switch ($radio) {
case 0:
$dateAfter = date('F jS, Y', strtotime('-1 week'));
break;
case 1:
$dateAfter = date('F jS, Y', strtotime('-1 month'));
break;
case 2:
$dateAfter = date('F jS, Y', strtotime('-2 year'));
break;
default:
$dateAfter = date('F jS, Y', 1970) ;
break;
}
}
$args = array(
'post_type' => 'any',
'post_status' => 'publish',
'orderby' => 'date',
'order' => $order,
's' => $query,
'date_query' => array (
array(
'after' => $dateAfter,
'before' => date('F jS, Y'),
'inclusive' => true
)
)
);
//nova variavel para objeto de query ordenada
$search = new WP_Query( $args );
//
ob_start();
//impressão do loop
print '<input type="hidden" id="foundPosts" value='.$search->found_posts.'>';
if($search->have_posts() ) {
print '<div class="row"><div class="col-md-12">';
while( $search->have_posts() ) : $search->the_post();
get_template_part( 'partials/search', 'geral' );
endwhile;
print '</div></div>';
wp_reset_postdata();
ajax_search_pagination($search);
}
var_dump($search);
//funcoes de encerramento
$content = ob_get_clean();
print $content;
die();
}
Page element creation function:
function ajax_search_pagination ($custom_query = false) {
if ( $custom_query ) $wp_query = $custom_query;
else global $wp_query;
$posted = max( 1, get_query_var('paged') );
if(is_paged() ) {
$posted = get_query_var('paged');
}
$big = 999999999; // need an unlikely integer
$items = '';
$format = 'page/%#%/'.get_search_query();
$pag_items = paginate_links( array(
//'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'base' => home_url( '/%_%' ),
// 'base' => '%_%',
//'format' => '?pagina=%#%',
'format' => $format,
'current' => $posted,
'total' => $wp_query->max_num_pages,
'before_page_number' => '<span class="sr-only">Página </span>',
'type' => 'array',
'add_args' => array(
'search_type' => 'geral',
's' => $custom_query->query_vars['s'],
// 'post_type' => $custom_query->query_vars['post_type'],
'order' => $custom_query->query_vars['order'],
//'orderby' => $custom_query->query_vars['orderby'],
'date_query' => array (
array(
'after' => $custom_query->query_vars['date_query'][0]['after'],
'before' => $custom_query->query_vars['date_query'][0]['before'],
'inclusive' => $custom_query->query_vars['date_query'][0]['inclusive']
)
)
)
) );
...
echo sprintf( '<nav class="text-center"><ul class="list-inline">%s</ul></nav>', $items );
}
}
I tried to cut some treatments that I considered irrelevant to be published, even in order not to leave the question too big. I hope the problem has been clear. Hugs