gpt4 book ai didi

php - Wordpress:搜索结果分页仍然不起作用

转载 作者:可可西里 更新时间:2023-11-01 01:01:08 34 4
gpt4 key购买 nike

我到处搜索解决方案并尝试了不同的解决方案,包括彻底解释的解决方案 here由 Chip Bennett 编写,但我似乎仍然无法正常工作。

结果的第一页工作正常,但从第 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/

最佳答案

您的主要查询和次要(自定义)查询之间存在冲突。

你做事一半错一半对:-)。放过你正在做的事情

  1. 代码的第一部分是更改主查询的正确方法。实际上,这就是使事情正常进行所需的全部。不过你这里有几个缺陷

    • 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 中设置paged 参数。这是由主查询设置的,不需要更改,因此请将其删除。

    • 除此之外,它应该可以正常工作。我无法测试您的代码,因为我没有与您相同的设置

  2. 您不需要自定义(辅助)查询。您已经修改了主查询以处理您的更改

  3. 您的循环很好。这就是您在 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:搜索结果分页仍然不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944940/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com