Custom Query Not Paginating

时间:2012-07-08 作者:Ty Morton

我有一个使用自定义查询筛选帖子的网站:

<?php //Search Region table to retrieve array of country names
$regionresult = mysql_query("SELECT * FROM gallery_regions WHERE region=\'$region\'") or die(mysql_error());
$num_rows = mysql_num_rows($regionresult);

// Process search reults and format array contents for WP Query
$country_search_array = array();
    while($row = mysql_fetch_array($regionresult)){
    $country_search_array[] = $row[\'country\']; 
        }

//Build WP query
$args = array(
     \'posts_per_page\' => \'48\',
     \'cat\' => \'4\',
     \'meta_query\' => array(
         array(
            \'key\' => \'Country\',
            \'value\' => $country_search_array,
            \'compare\' => \'IN\'
        )
    )
);
$gallery_query = new WP_Query( $args );?>
通过使用WP页码插件对结果分页的自定义函数进行补充(http://www.jenst.se/2008/03/29/wp-page-numbers):

function wp_page_numbers_custom( $wp_query, $start = \'\', $end = \'\' )
{
    $_backup = array(
        \'wp_query\' => @ $GLOBALS[\'wp_query\'],
        \'max_page\' => @ $GLOBALS[\'max_page\'],
           \'paged\' => @ $GLOBALS[\'paged\'],  
    );

    $GLOBALS[\'wp_query\'] = $wp_query;
    $GLOBALS[\'max_page\'] = $wp_query->max_num_pages;
       $GLOBALS[\'paged\'] = max( $wp_query->get( \'paged\' ), 1 );

    wp_page_numbers( $start, $end );

    foreach ( $_backup as $var => $val )
        $GLOBALS[ $var ] = $val;
}

//usage: wp_page_numbers_custom( $gallery_query );
虽然返回的页数是准确的,但导航不起作用。如果单击第2页(共X页),尽管URL发生了更改,但仍会返回第1页。

可以找到有问题的站点here.

非常感谢您的帮助。

谢谢

1 个回复
最合适的回答,由SO网友:Milo 整理而成

您需要设置pagedoffset 在您的查询中,否则您总是获取相同的48篇帖子。

$page = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
$args = array(
 \'paged\' => $page,
 \'posts_per_page\' => \'48\',
 \'cat\' => \'4\',
 \'meta_query\' => array(
     array(
        \'key\' => \'Country\',
        \'value\' => $country_search_array,
        \'compare\' => \'IN\'
     )
  )
);
$gallery_query = new WP_Query( $args );?>

结束

相关推荐

Pagination not working

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