I'm trying to create a custom search page using wp_query. I'm using post method to get search values. At first, the search was doing fine, but when i got a search results of more than 4 posts (i set 4 posts displayed per page) and tried getting to the second page of the results, it returns to the normal query results (without the search values). Here's my code:

if (isset($_POST['btn_search'])) {

    $by_year = $_POST['sel_year'];
    $by_platform = $_POST['sel_platform'];
    $by_title = $_POST['txt_title'];
    $by_genre = $_POST['txt_genre'];

    if ($by_year!=""&&$by_platform!="") {
        $by_year = $by_year."+".$by_platform;


I'm combining the platform and year fields because they're both in the category Here's the query:

$currentPage = get_query_var('paged');

    $allGames = new WP_Query(array(
        'category_name' => $by_year,        
        'posts_per_page' => 4,
        'tag' => $by_genre,
        'paged' => $currentPage,
        's' => $by_title


    if ($allGames->have_posts()) {

        while ($allGames->have_posts()) {
            $row_start = "";
            $row_end = "";


            if ($row_counter!=2) {
             $row_start = '<div class="row"><div class="card-deck">';

            } else {

            if ($post_count==$allGames->post_count) {

            echo $row_start;
            echo '<div class="col-md-6 col-sm-12 text-center">
                    <!-- Card -->
                    <div class="card special-color">
                      <!-- Card image -->
                      <div class="view overlay zoom">
                          <img class="card-img-top" src="'.get_the_post_thumbnail_url().'" alt="Card image cap">
                          <a href="#!">
                            <div class="mask rgba-white-slight"></div>
                      <!-- Card content -->
                      <div class="card-body">
                        <!-- Title -->
                        <h2 class="card-title"><a class="text-white-50" href="'.get_the_permalink().'">'.get_the_title().'</a></h2>
                        <!-- Text -->
                        <p class="card-text">'.get_the_excerpt().'</p>
                        <!-- Button -->
                        <a href="#" class="btn blue-grey btn-block">View</a>
                    <!-- Card -->
            echo $row_end;




        <div class="text-center">
        echo paginate_links(array(
            'total' => $allGames->max_num_pages


I hope to get an answer ASAP. I need it badly :(. Thanks in advance!

1 Answer 1


You'll have a much easier time if you use the main query, and add your extra filters with pre_get_posts.

So first of all, just use search.php as your template, and use the main query. So no new WP_Query(), and use the have_posts() and the_post() functions, not the methods.

So your template (simplified) would be like this:

if ( have_posts() ) {
    while ( have_posts() ) {
        // etc.

echo paginate_links();


Then change the txt_title field to use the native s name for searches.

Then, to handle the extra search parameters, use the pre_get_posts hook:

function wpse_338980_search( $query ) {
    if ( is_admin() ) {

    if ( $query->is_search() ) {
        $query->set( 'posts_per_page', 4 );

        if ( isset( $_POST['sel_year'] ) && isset( $_POST['sel_platform'] ) ) {
            $query->set( 'category_name', $_POST['sel_year'] . '+' . $_POST['sel_platform'] );

        if ( isset( $_POST['txt_genre'] ) ) {
            $query->set( 'tag', $_POST['txt_genre'] );
add_action( 'pre_get_posts', 'wpse_338980_search' );

Doing it that way means that the main query is being properly filtered according to your search parameters, and because it's the main query the pagination functions will work out of the box.

  • Thank you for the suggestion! Gonna try that now and see what happens. Thank you so much! Btw, i'm new to wordpress so sometimes i get too confused about it.
    – Perl
    May 28, 2019 at 13:05
  • How can i use the pre_get_posts? or the function that you stated there. Sorry, i'm really new to wordpress
    – Perl
    May 28, 2019 at 13:13
  • The 2nd block of code would go in your theme'/child theme's functions.php file. May 28, 2019 at 13:15
  • Ok thanks, just one more thing. Will it work when i'm using a custom page template? it's in a page called browse games and i'm using a page-browse-games.php file. Thank you so much for your time
    – Perl
    May 28, 2019 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.