ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP-WordPress:搜索结果分页仍然无法正常工作

2019-10-29 04:28:59  阅读:231  来源: 互联网

标签:search pagination wordpress php


我一直在寻找解决方案的高低,并尝试了不同的解决方案,包括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

标签:search,pagination,wordpress,php
来源: https://codeday.me/bug/20191029/1957635.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有