1

I have a custom search query which I have put in a plugin. The homepage has a custom search form and when the user searches for something, it goes to the search page and displays the result.

My issue is that the custom query runs on every page of the website, even in the admin panel. It keeps throwing the error latitude and longitude not defined on every page.

I have used is_search() function to make sure the function runs only on search pages but it doesn't work. It still runs the query!

This is the code:

        /**
         * Custom Search on Homepage
         *
         * 
         */
        add_filter('posts_fields', 'distance_query'); 
        add_filter('posts_where', 'lat_lng_define');
        add_filter('posts_join', 'lat_lng_join');
        add_filter('posts_groupby', 'having_distance');
        add_filter('posts_orderby', 'sort_distance');

        /**
         * The Fields
         *
         * 
         */
        function distance_query( $fields ) {
            global $wpdb;

            //if(isset($_GET['latitude'] )|| isset($_GET['longitude'] )) {
                $lat = sanitize_text_field( $_GET['latitude'] );
                $lng = sanitize_text_field( $_GET['longitude'] );
            //} else {
            //  return;
            //}


           if (is_search())

               $fields .= ", 
           SOME QUERY"

            return $fields;
          }

/**
 * The Join
 *
 * 
 */
  function lat_lng_join( $join) {
    global $wpdb;
if (is_search())
     $join = " QUERY";
    return $join;
  }


    //Likewise the rest of the query

If I isset the $GET values, the homepage shows 404:

 if(isset($_GET['latitude'] )|| isset($_GET['longitude'] )) {
            $lat = sanitize_text_field( $_GET['latitude'] );
            $lng = sanitize_text_field( $_GET['longitude'] );
        } else {
          return;
        }

The search query running on every page is making my website too slow. Any assistance would be appreciated.

  • What if you place the $lat and $lng within the is_search() conditional? – czerspalace Feb 27 '17 at 18:22
  • @czerspalace I get a 404 on homepage – input Feb 27 '17 at 18:52
  • 1
    As it is, you have a syntax error in your conditional. You are missing the curly brackets {} so the query is modified regardless of the if statement. If this is not a copy/paste error, try to put if( is_search() ) { $fields = ...; return $fields } same thing with your $join variable. – bynicolas Feb 27 '17 at 19:43
  • 1
    No it won't... Did you read the answer correctly? If you have more than one statement in your if, it will break in funny ways to paraphrase the answer you linked to. And guess what, your code is breaking in funny ways ! So while technically not a syntax error, the syntax is wrong in the way you are using it. An if without the {} will consider ONLY the next statement as part of the condition. – bynicolas Feb 27 '17 at 22:05
  • 1
    @bynicolas, thanks for pointing out. I feel stupid now. This was it. Can you please post your comment as answer so I can accept it? Thanks. – input Mar 2 '17 at 11:30
2

You are missing the curly brackets {} around your query statements so the query is modified regardless of the if statement.

So try modifying your code to have something like

if( is_search() ) { 
  $fields = '...'; 
  return $fields 
} 

same thing with your $join variable

Remember that while using if without brackets is acceptable for single statements, it is better to always use the curly brackets. Even if it's only to make the code more consistent and easy to read for another coder (or even you in 6 months from now).

You'll save an awful lot of time debugging errors that aren't actually ones. A little bit like you just experienced with your issue here :)

0

try adding more conditional checks as required.

if ( is_search() && ! is_admin && ! is_front_page() && ! is_page() ) {

   $fields .= "SOME QUERY";

  return $fields;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.