自定义搜索与结果不一致

时间:2014-04-14 作者:MxmastaMills

我已设置自定义搜索以使用s 获取URL上的变量。我希望它能取得一定的效果,但我遇到了一个奇怪的问题。首先,这是我的代码:

$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);
        }
    }
}
本质上,我为每个帖子创建一个唯一值数组,因为我需要在这之后立即将它们用于函数。

我有一篇题为“厨房助理”的帖子当我搜索“厨房”时,它出现了。当我搜索“助手”时,它不会显示。

我有其他帖子的标题和内容中有“助手”一词,这些帖子在我搜索“助手”时会显示出来。我很好奇为什么它会显示一个搜索词而不是另一个?

任何帮助都将不胜感激。谢谢

编辑以下是我在更大背景下的代码,以供澄清。我已经创建了一个独立的模板PHP页面作为新的搜索页面,因为我正在进行自定义。这是一个搜索表单,可以搜索两个不同的术语。一个是搜索词,另一个是位置词。我首先对搜索词进行WordPress查询,并将它们全部添加到一个数组中(如果存在搜索词)。然后我搜索该位置(如果该位置存在)。然后,如果两者都存在,我会交叉引用它们,否则我只会根据存在的变量进行搜索。除了WordPress中的搜索部分之外,一切似乎都正常。它根本不会影响位置搜索,而且位置搜索似乎与Wordpress查询无法提取正确变量的原因并不相关,但每个人都在不断询问它的上下文,所以我想我会与大家分享。让我知道这是否能更清楚地说明情况,或者你是否希望我澄清其中的任何其他因素。谢谢

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

1 个回复
最合适的回答,由SO网友:MxmastaMills 整理而成

我终于找到了自己问题的答案。我使用的是九五模板,我想在默认情况下,返回的搜索结果数量是有限制的(我不确定这是否是WordPress的默认值;也许有人可以澄清这一点)。

我将查询更改为:

$s = new WP_Query(array(\'s\' => $search_term, \'posts_per_page\' => -1));
不限制搜索结果,这改变了它。我需要在第1页上找到所有可能的搜索词,以便将其与位置进行交叉引用,但它正在“分页”搜索结果。

谢谢你的帮助!

结束

相关推荐

Custom Same Page Search

我已将自定义表单添加到我的一个页面模板中。它屈服于自己$_SERVER[\'REQUEST_URI\'] 希望我能抓住$_GET 值并使用修改我的查询pre_get_posts, 但它却转到了404。因此,为了澄清,我不想使用搜索。php,我希望它发回我的页面模板,并修改我的查询以显示“filterd”结果,而不是404ing。我试过使用pre_get_posts 测试是404还是搜索,但我也不想阻止404的任何其他操作。那么,我如何在同一页面/url/页面模板上使用查询,而无需回复search.php