使用$ _GET和分页显示相同的帖子

时间:2017-01-27 18:22:32

标签: php wordpress pagination

我正在使用$_GET为我的搜索结果更新一些变量,但当我点击结果的下一页时,它只是从第1页再次向我显示相同的帖子。我似乎无法弄清楚问题是什么

<?php
if(isset($_GET['submit'])){
    $carigot = $_GET['car'];
    $searchm = $_GET['s'];
}
?>

<?php

$custom_query_args = array(
    'post_type'  => 'post',
    'posts_per_page' => '1',
    'meta_query' => array(
        array(
            'key'     => 'CurrentLocation',
            'value'   => $searchm,
            'compare' => '=',
        ),
        array(
            'key' => 'CarIWant',
            'value'   => $carigot,
            'type'    => '',
            'compare' => '=',
                ),
    ),
    'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);

 $custom_query = new WP_Query( $custom_query_args ); 

$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query; 

if ( $custom_query->have_posts() ) :
    while ( $custom_query->have_posts() ) :
        $custom_query->the_post();
      get_template_part( 'template-parts/post/content', 'excerpt' );
    endwhile;
endif;
// Reset postdata
wp_reset_postdata();

next_posts_link( 'Next', $custom_query->max_num_pages ); 
previous_posts_link( 'Previous' );


 $wp_query = NULL;
$wp_query = $temp_query;
?>

1 个答案:

答案 0 :(得分:0)

您正在覆盖您的查询。请注意,您两次调用new WP_Query并将其分配给同一个变量。将您的分页移动到原始分区并摆脱第二个分页。

$custom_query_args = array(
        'post_type'  => 'post',
        'posts_per_page' => '1',
        'meta_query' => array(
            array(
                'key'     => 'CurrentLocation',
                'value'   => $searchm,
                'compare' => '=',
            ),
            array(
                'key' => 'CarIWant',
                'value'   => $carigot,
                'type'    => '',
                'compare' => '=',
                    ),
        ),
        'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);