我一直在寻找解决方案的高低,并尝试了不同的解决方案,包括Chip Bennett彻底解释过的here,但我似乎仍然无法使它正常工作.
结果的第一页工作正常,但是从第2页开始,它仅显示索引模板,但仍显示未找到页面.这是我的代码:
Functions.PHP
function advanced_search_query($query) {
if ($query->is_search) {
$query->set('s', $_GET['s']);
$query->set('post_type', array( 'properties' ));
$query->set('Meta_key', $_GET['Meta_key']);
$query->set('orderby', $_GET['sortby']);
$query->set('order', $_GET['order']);
$query->set('posts_per_page', '5');
$query->set('paged', $paged);
if (isset($_GET['author'])) {
$query->set('author', $_GET['author']);
}
if (isset($_GET['propertytype'])) {
$query->set('taxonomy', 'propertytype');
$query->set('terms', $_GET['propertytype']);
}
$minCost = $_GET['minCost'];
$minCost = preg_replace("/[^0-9]/","", $minCost);
if ($minCost == ""){
$minCost = "0";
}
$maxCost = $_GET['maxCost'];
$maxCost = preg_replace("/[^0-9]/","", $maxCost);
if ($maxCost == ""){
$maxCost = "99999999999999";
}
$query->set('Meta_query', array(
'relation' => 'AND',
array(
'key' => 'ce_location',
'value' => $_GET['location'],
'compare' => 'LIKE',
'type' => 'CHAR'
),
array(
'key' => 'ce_cost',
'value' => array($minCost, $maxCost),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'ce_bedrooms',
'value' => array($_GET['minbedrooms'], $_GET['maxbedrooms']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'ce_tenancy',
'value' => $_GET['tenancy'],
'compare' => 'LIKE',
'type' => 'CHAR'
)
));
};
return $query;
};
add_filter('pre_get_posts', 'advanced_search_query', 1000);
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
}
$search_query['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$search = new WP_Query($search_query);
循环码:
if ( have_posts() ) : while (have_posts()) : the_post();
//loop content
endwhile;
if(function_exists('wp_simple_pagination')) { wp_simple_pagination(); } ?>
else :
echo 'Sorry, there are currently no property listings available';
endif;
任何建议将不胜感激.
编辑:
这是第1页的网址:
http://localhost/cunningham/?location=&propertytype=&minbedrooms=1&maxbedrooms=9&minCost=0&maxCost=100000&Meta_key=&tenancy=&s=
第2页网址:
http://localhost/cunningham/page/2/?location&propertytype&minbedrooms=1&maxbedrooms=9&minCost=0&maxCost=100000&Meta_key&tenancy&s/
解决方法:
您所做的事情错了一半,错了一半:-).让我们看看你在做什么
>代码的第一部分是更改主查询的正确方法.实际上,这是使一切正常运行所需的全部.你这里有几个缺点
> pre_get_posts确实会更改所有查询,无论是主查询还是辅助查询.而且这不仅发生在前端,后端也受到影响.您将只需要将更改应用于前端,并且仅针对主要查询
function advanced_search_query($query) {
if ( !is_admin() && $query->is_main_query() && $query->is_search() ) {
//REST OF YOUR CODE
>您无需在pre_get_posts中设置分页参数.这是由主查询设置的,不需要更改,因此将其删除.
>除此之外,它应该可以正常工作.我无法测试您的代码,因为我的设置与您的设置不同
>您不需要自定义(二级)查询.您已经修改了主查询以处理您的更改
>您的循环很好.这就是您在search.PHP模板中所需要的,仅此而已
编辑
从OP的评论
I’m pretty much assuming that there is a permalink issue somewhere. I don’t have anything in my
.htaccess
besides the basics and my permalink structure is/%category%/%postname%/
and with that structure pagination works all fine on another page that has a normal query hardcoded into the PHP file. It is literally only this search page having issues and it’s all because paged isn’t being set it the URL
和解决方案
Well, I found the solution. The trailing slash was messing everything up
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。