WP_Query breaks pagination

时间:2012-11-17 作者:zilj

我已经研究这个问题一周了。我搜索了无数没有回答这个问题的帖子。我的wp\\u查询似乎破坏了分页。我已经在这里浏览了很多关于wp\\U查询和分页的帖子,但仍然毫无乐趣。

据我所知,它的设置是正确的。我有一个实例,它将在一个页面中显示所有帖子,而在另一个页面中,它会重复每个页面上的内容。请参阅下面的代码,以获取带注释的精简版本。

有人知道问题出在哪里吗?

<?php 
/* 

    Let\'s asume that this is the search query in the address bar: 
    ?s=media&trend=&industry=&d=2012-05-17|2012-11-17
    (To iterate, it\'s saying search for keyword \'media\' 
    posted between 17th May 2012 and 17th Novemer 2012)

    It gives 13 results and shows all on the same page. 
    I have set show 10 posts per page so it should be cutting off 
    at first 10. 

    PAGINATION: On line 56 If I change the code to 
    $wp_query->max_num_pages, the pagination works but then 
    repeats the same 10 posts. 

*/

// Setup pagination
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

// Sort trend taxonomy
$tr = $_GET[\'trend\']; 
// Sort industry taxonomy
$ind = $_GET[\'industry\']; 
// Set date var
$d = $_GET[\'d\']; 

$args = array(
    \'post_type\' => \'post\', 
    \'paged\' => $paged,
    \'posts_per_page\' => 10,
    \'trend\' => $tr,
    \'industry\' => $ind
);  
// Date range function, if available                        
if( (isset($_GET[\'d\'])) && ($_GET[\'d\']!==\'\') ) {
    function filter_where( $where = \'\' ) {
        $date_range = $_GET[\'d\'];
        $date_range = explode("|", $date_range);
        $that_date = $date_range[0]; 
        $this_date = $date_range[1];                                
        $where .= " AND post_date >= \'".$that_date."\' AND post_date <= \'".$this_date."\'";
        return $where;
    }
    // Add the filter 
    add_filter( \'posts_where\', \'filter_where\' );
}   

// setup the wp_query
$allsearch = new WP_Query($args); 

// Remove the date filter where clause
if( (isset($_GET[\'d\'])) && ($_GET[\'d\']!==\'\') ) {
    remove_filter( \'posts_where\', \'filter_where\' ); 
}

while ( $allsearch->have_posts() ) : $allsearch->the_post(); 
    // Show the posts
endwhile;

// 
if ( $allsearch->max_num_pages > 1 ) : ?>
    <div class="nextprev pagination">
    <div class="nav-previous"><span class="nextprev-arrow">&lsaquo;</span> <span class="nextprev-link-title">Older posts</span></div>
    <div class="nav-next"><span class="nextprev-arrow">&rsaquo;</span> <span class="nextprev-link-title">Newer posts</span></div>
    </div><!-- .nextprev -->
<?php endif; ?>
<?php wp_reset_query(); ?>

1 个回复
SO网友:Javier

如果您想继续使用新的Wp查询,如您的示例所示,将查询变量的$allsearch名称更改为$Wp\\u Query应该可以做到这一点。分页函数似乎希望查询的变量名。。。所以,试试。。。

结束

相关推荐

Pagination not working

$paged for pagination是awlays返回1,因此无论我单击哪个页面,“1”总是突出显示,并且总是显示相同的帖子。我尝试了一百万种不同的东西组合来修复,但没有任何效果:(<ul> <?php wp_reset_query(); $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;&#