Wordpress: search results are not working yet

I've searched high and low for solutions and tried different ones, including the one explained in detail here by Chip Bennett, but I still can't get it working.

The first page of results works fine, but from page 2, it only displays the index template and still says the page was not found. Here's my code:

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);

      

Code for displaying query arguments

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);

      

Loop code:

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;

      

Any suggestions would be much appreciated.

EDIT:

I also noticed that the url changed when I try to access page 2.

This is the URL of the page:

http://localhost/cunningham/?location=&propertytype=&minBedrooms=1&maxBedrooms=9&minCost=0&maxCost=100000&meta_key=&tenancy=&s=

      

Page URL:

http://localhost/cunningham/page/2/?location&propertytype&minBedrooms=1&maxBedrooms=9&minCost=0&maxCost=100000&meta_key&tenancy&s/

      

+3


source to share


1 answer


You have a clash between your main query and your secondary (regular) query.

You are doing things half wrong and half right :-). Skip what you are doing.

  • The first section of code is the correct method to change the main query. That's actually all you need to get it to work. You have a couple of disadvantages though

    • pre_get_posts

      modifies all queries, primary and secondary queries. And this happens not only on the front side, but also on the back end. You will only need to apply the changes to the frontend and only configure the main query

      function advanced_search_query($query) {
          if ( !is_admin() && $query->is_main_query() && $query->is_search() ) {
      
             //REST OF YOUR CODE
      
            

    • You don't need to set the parameter paged

      to pre_get_posts

      . This is set by the main query and does not need to be changed, so delete it.

    • Plus, it should work fine. I cannot test your code as I do not have the same setup as you

  • You don't need your custom (secondary) query. You have already changed your main request to handle your changes.

  • Your loop is perfect. That's all you need in your search.php template, nothing else

EDIT



From OP comments

I'm pretty sure there is a permalink issue somewhere. I have nothing in my .htaccess

besides the basics and my structure is permalink /%category%/%postname%/

and with this structure pagination works fine on another page that has a normal request encoded into a php file. It's literally just this search page having problems, and it's all because there is no page-by-page URL

And the solution

Well I found a solution. The trailing slash ruined everything.

+3


source







All Articles