1

I've set up a custom search to use the s GET variable on a URL. I want it to fetch certain results but I've come across an odd problem. First, here's my code:

$search_term = $_GET['s'];

if($search_term!=''){
    $s = new WP_Query(array('s' => $search_term));
    $search_array = array();

    if($s->have_posts()){
        while($s->have_posts()){
            $s->the_post();

            $title = get_the_title();
            $permalink = get_permalink();
            $search_identifier = $title.$permalink;

            array_push($search_array,$search_identifier);
        }
    }
}

Essentially, I'm creating an array of unique values for each post because I need to use them for a function immediately after that.

I have a post titled 'Kitchen Assistant.' When I search 'kitchen', it shows up. When I search 'assistant' it does not show up.

I have other posts with the word 'assistant' in the title and content and those show up when I search 'assistant'. I'm curious as to why it would show up with one search term and not the other?

Any help would be greatly appreciated. Thanks!

EDIT

Here is my code in a greater context for clarification. I've created a standalone template PHP page as a new search page because of the customizations I am looking to do. It's a search form that searches for two different terms. One is a search term and the other is a location term. I first do a WordPress query for the search term and add them all to an array (if a search term exists). I then do a search for the location (if the location exists). I then cross-reference them if both exist, otherwise I just do a search depending on the variable that exists. It all seems to work EXCEPT the search part in WordPress. It doesn't affect the location search at all and the location search doesn't really seem relevant to why the Wordpress query is failing to pick up the right variables but everyone keeps asking about the context of it so I figured I'd share. Let me know if this sheds more light on the situation or if you'd like me to clarify any other elements of it. Thanks!

<?php
//DIRECT CONNECTION TO DATABASE
include('job-parsing/connect.php');
//WP CONNECTION TO DATABASE
include('wp-config.php');

get_header();

$search_term = $_GET['s'];
$real_loc = $_GET['location'];
$location = urlencode($_GET['location']);

//QUERY SEARCH TERM INITIALLY
if($search_term!=''){
    $s = new WP_Query(array('s' => $search_term));
    $search_array = array();

    if($s->have_posts()){
        while($s->have_posts()){
            $s->the_post();
            //var_dump($s);

            //I ADD POST LONGITUDE AND LATITUDE FIELDS BECAUSE THESE POSTS ARE ENTERED DAILY ON A CRON JOB WITH THOSE VALUES; THESE WILL BE NEEDED LATER DURING A GEOLOCATION TEST BELOW
            $post_latitude = get_post_field('post_latitude');
            $post_longitude = get_post_field('post_longitude');

            //CURRENTLY SOME POSTS DON'T HAVE LONGITUDE AND LATITUDE SO I DON'T WANT TO HAVE THOSE INCLUDED IN THE SEARCH, THEY ARE IN A LIST ON THE SITE BUT DON'T HAVE LOCATIONS SO IT DEFEATS THE PURPOSE OF WHAT WE ARE DOING AND WILL BE EXCLUDED FROM SEARCH
            if($post_latitude && $post_longitude){
            $id = get_the_ID();
            $search_identifier = $id;

            //IF POST LATITUDE AND POST LONGITUDE EXIST ON TERM THAT MATCHES SEARCH TERM
            array_push($search_array,$search_identifier);
            }   
        }
    }
}

//SEARCH RESULTS ARRAY IS NOW SET

//IF LOCATION IS FILLED OUT, RUN GOOGLE API CODE (POSTCODE/TOWN IS THE DEFAULT VALUE/PLACEHOLDER ON THE FORM SO IN THE CHANCE THAT SOMEONE SUBMITS WITH THAT STILL IN THE INPUT, I DON'T WANT IT TO RUN THROUGH THE GOOGLE API
if($location!="Postcode%2FTown" && $location!="" && $location!="Postcode/Town"){
    //TAKE THE LOCATION SEARCH TERM AND CONVERT IT INTO LATITUDE AND LONGITUDE COORDINATES
    $url='http://maps.googleapis.com/maps/api/geocode/json?address='.$location.'&sensor=false';
    $source = file_get_contents($url);
    $obj = json_decode($source);
    $LATITUDE = $obj->results[0]->geometry->location->lat;
    $LONGITUDE = $obj->results[0]->geometry->location->lng;

    $center_lat = $LATITUDE;
    $center_lng = $LONGITUDE;
    $radius = $_GET['radius'];

    $query = sprintf("SELECT ID, post_title, post_latitude, post_longitude, post_date, ( 3959 * acos( cos( radians('%s') ) * cos( radians( post_latitude ) ) * cos( radians( post_longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( post_latitude ) ) ) ) AS distance FROM wp_posts HAVING distance < '%s' ORDER BY distance",
      $mysqli->real_escape_string($center_lat),
      $mysqli->real_escape_string($center_lng),
      $mysqli->real_escape_string($center_lat),
      $mysqli->real_escape_string($radius));
    $result = $mysqli->query($query);
    $found_num = mysqli_num_rows($result);
//END GOOGLE MAPS API CALL
}
?>

<div class="focus">
  <div class="whiteboard">
    <div>
        <h2><strong><?php echo $found_num; ?></strong><span class="jobs-text"> Jobs found for <strong><?php echo $real_loc; ?></strong>"</h2>
</div>

//BEGIN JOB LIST
<ol class="list">

<?php
//IF LOCATION WAS ENTERED AND A SEARCH QUERY USING GOOGLE MAPS API WAS USED SUCCESSFULLY
if($result){
//INITIALIZE NEW ARRAY
    $new_search_array = array();
    while ($row = $result->fetch_assoc()){
        $post_id = $row['ID'];
        $permalink = get_permalink($post_id);
        $title = $row['post_title'];

        //IF SEARCH TERM EXISTS, CREATE ARRAY THAT MATCHES LOCATION AS WELL
        if($search_array){
            foreach($search_array as $value){
                if($value==$post_id){
                    array_push($new_search_array,$post_id);
                }
            }
        }
        //ELSE IF SEARCH DOESN'T EXIST, JUST DO LOCATION SEARCH
        else{
        echo $row['post_title'];
        }
    }
    }

    //IF SEARCH TERM DID EXIST AND LOCATION TERM DID EXIST, NEW SEARCH ARRAY INITIALIZED AND WE USE THAT TO DISPLAY RESULTS    
    if($new_search_array){
        foreach($new_search_array as $value){
            get_the_title($value);
        }
    }
    else if($search_array){
    foreach($search_array as $value){
        get_the_title($value);
    }
}
?>

</ol>

<?php
get_footer();
?>
  • var_dump($s) and see if the posts are represented before you do any processing. Second, the results should already be unique and the post ID should be a unique identifier. I don't understand the point of this code. – s_ha_dum Apr 14 '14 at 13:45
  • I'll do the var_dump(). I echo'ed out the titles on this though and they don't show up so I'm assuming they are not represented. There's a bunch more to this code that would be too much to put here. I'm having to process it against a complete separate query from something else which is why I'm making the unique identifier. – MxmastaMills Apr 14 '14 at 20:20
  • So it is not being represented in the var_dump($s). It should up in the dump for 'kitchen' but not for 'assistant'. Why would that be? – MxmastaMills Apr 15 '14 at 18:25
  • Can I ask why you're doing a WP_Query? 's' is a query var and WP will already have figured out that you intend to do a search, the WP_Query is completely unnecessary as it's duplicating the exact same query that WP has already done for the main loop. Also if you could explain what you're actually trying to do, I suspect this is an XY problem, and you're trying to debug your solution rather than explaining the original task/problem you set out to solve – Tom J Nowell Apr 15 '14 at 19:06
  • Well I'm doing a double search. One is for search terms and one is for location. If the location field is filled out and the $s filed is not (which is not in my code), then it runs through the Google Maps API and returns results. All of the posts have location markers on them. If you run a search that has $s but no location, it runs a normal. If you run both, it runs the $s and then checks those posts if they match the location criteria. What do you mean by XY problem? – MxmastaMills Apr 15 '14 at 21:40
1

I figured out the answer to my own question (finally!). I'm using the NineToFive template and I guess by default there's a limit on the number of search results that's returned (I'm not sure if that's a WordPress default; maybe someone can clarify that).

I changed my query to this:

$s = new WP_Query(array('s' => $search_term, 'posts_per_page' => -1));

To not put a limit on the search results and that changed it. I needed to find every possible search term on page 1 in order to cross reference it with the location but it was 'paginating' the search results.

Thanks for your help!

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.