最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - paginate_links method doesn't show second page of my custom wp_query

programmeradmin1浏览0评论

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_counter++;
            $post_count++;
            $row_start = "";
            $row_end = "";

            $allGames->the_post();

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

            } else {
                $row_end='</div></div><br>';
                $row_counter=0;
            }

            if ($post_count==$allGames->post_count) {
                $row_end='</div></div><br>';
                $row_counter=0;
                $post_count=0;
            }

            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>
                          </a>
                      </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>
                      </div>
                    </div>
                    <!-- Card -->
                </div>';
            echo $row_end;

            }


        }


        ?>

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

        wp_reset_postdata();
        ?>
        </div>

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

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_counter++;
            $post_count++;
            $row_start = "";
            $row_end = "";

            $allGames->the_post();

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

            } else {
                $row_end='</div></div><br>';
                $row_counter=0;
            }

            if ($post_count==$allGames->post_count) {
                $row_end='</div></div><br>';
                $row_counter=0;
                $post_count=0;
            }

            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>
                          </a>
                      </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>
                      </div>
                    </div>
                    <!-- Card -->
                </div>';
            echo $row_end;

            }


        }


        ?>

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

        wp_reset_postdata();
        ?>
        </div>

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

Share Improve this question edited May 28, 2019 at 13:58 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 28, 2019 at 12:30 PerlPerl 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

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() ) {
        the_post();
        // etc.
    }
}

echo paginate_links();

wp_reset_postdata();

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() ) {
        return;
    }

    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.

发布评论

评论列表(0)

  1. 暂无评论