wp query – wp_query ‘s’, search filter with pagination is not working

Question

I made this shortcode for a page of posts.
I want to set a search function.
I filled for example: “web design”.
The search-result will set in the var “$catSearched”. Then it works how i want.
But! when i click the next page, it doesn’t get the variable $catSearched.
For example:
when i clikck next button(a href), then i lost the result in https://www.myexampledomain.com/postpages/page/2

Because, of course the var $catSearched will reset, when i move to another page.
How can i keep the search result..!?

Here is my codes:

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { 
    $paged = get_query_var('page');
} else {
    $paged = 1;
}


if ( isset($_POST['submit_checkbox']) ) :
    if ( !empty($_POST['input_search']) ) : 
        $catSearched =  $_POST['input_search'];
    endif; 
endif;



$html .= '<form method='POST'>';
$html .= '<label>Search here: </label>';
$html .= '<input type='text' name='input_search' />';
$html .= '<button id='cat--submit' type='submit' name='submit_search' value='Submit' />It is Btn</button>';
$html .= '</form>';


$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 5,
    'paged' => $paged,
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
    's' => $catSearched,
    'order' => 'DESC',
    'orderby' => 'date'
);

$my_query = new WP_Query( $args );

$html .= '<ul>';
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
        $html .= '<li>';
        $html .= '<h3>' . get_the_title() . '</h3>';
        $html .= '</li>';
    endwhile;
$html .= '</ul>';

    $pageAll = $my_query->max_num_pages;

    if ($pageAll > 1) :
        $orig_query = $wp_query; // fix for pagination to work
        $wp_query = $my_query;

        // PREV
        $prevBtn = get_previous_posts_link( 'Prev' )

        // NEXT
        $nextBtn = get_next_posts_link( 'Next', $pageAll )
                    
        $html .= '<nav>';
        $html .= '<span class='prev'>'. $prevBtn .'</span>';
        $html .= '<span class='next'>'. $nextBtn .'</span>';
        $html .= '</nav>';

        $wp_query = $orig_query;
    endif;


    wp_reset_postdata();
endif;


return $html;

0
고준혁 4 weeks 2021-11-03T11:41:11-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse