PHP-WordPress:搜索结果分页仍然无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-WordPress:搜索结果分页仍然无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3733字,纯文字阅读大概需要6分钟。
内容图文
![PHP-WordPress:搜索结果分页仍然无法正常工作](/upload/InfoBanner/zyjiaocheng/680/4c27b6777e8b47d1821f9b0bbb627fa6.jpg)
我一直在寻找解决方案的高低,并尝试了不同的解决方案,包括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;
任何建议将不胜感激.
编辑:
我还注意到当我尝试访问页面2时URL已被修改.
这是第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
内容总结
以上是互联网集市为您收集整理的PHP-WordPress:搜索结果分页仍然无法正常工作全部内容,希望文章能够帮你解决PHP-WordPress:搜索结果分页仍然无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。