2017-02-10 65 views
1

我有一個搜索字段,它允許用戶搜索地點和郵政編碼。當用戶輸入搜索值時,我有兩個隱藏的輸入字段,根據用戶選擇填充經度和緯度。在Wordpress中更改搜索查詢

我想運行一個查詢其搜索與隱藏輸入值來帖 - latitudelongitude什麼類型的用戶在

<form role="search" method="get" id="searchform" class="searchform" action=""> 
<div class="search"> 
<input type="text" value="" name="s" id="search-input" class="s" placeholder="Enter your Location or Postcode" /> 
<input type="hidden" value="profile" name="post_type" id="post_type" /> 
<input type="hidden" value="country" name="taxonomy" /> 
<input type="hidden" name="latitude" id="latitude" /> 
<input type="hidden" name="longitude" id="longitude"/> 
<input type="submit" id="searchsubmit" value="Search" class="submit btn" /> 
</form> 

我怎麼能替換搜索查詢完全與另一個查詢搜索帖子根據用戶選擇的經緯度?

這是SQL查詢,我想,以取代主查詢:

SELECT SQL_CALC_FOUND_ROWS wp_posts. * 
FROM wp_posts 
WHERE 1 =1 
AND wp_posts.ID 
IN (

SELECT post_id 
FROM lat_lng_post 
WHERE (3959 * ACOS(COS(RADIANS(- 27.922459)) * COS(RADIANS(lat)) * COS(RADIANS(lng) - RADIANS(153.334793)) + SIN(RADIANS(- 27.922459)) * SIN(RADIANS(lat)))) <=125 
) 
LIMIT 0 , 30 

我有一個函數準備好,但我不知道這WordPress的鉤,我應該用它來改變搜索查詢另一個without modifying the search template

function custom_search() 
{ 

    global $wpdb; 

    if (is_search()) 
    $latitude = filter_input(INPUT_GET, "latitude", FILTER_SANITIZE_STRING); 
    $longitude = filter_input(INPUT_GET, "longitude", FILTER_SANITIZE_STRING); 
    $radius = 125; 

    $sql = "SELECT SQL_CALC_FOUND_ROWS wp_posts. * 
       FROM wp_posts 
       WHERE 1 = 1 
       AND $wpdb->posts.ID IN (SELECT post_id FROM lat_lng_post WHERE 
       (3959 * acos(cos(radians(%f)) 
           * cos(radians(lat)) 
           * cos(radians(lng) 
           - radians(%f)) 
           + sin(radians(%f)) 
           * sin(radians(lat)))) <= %s)"; 

    $sql = $wpdb->prepare($sql, $latitude, $longitude, $latitude, $radius); 

    //$wpdb->show_errors(); 
    $results = $wpdb->get_results($sql); 
    //$wpdb->print_error();  


    return $results; 

} 

任何幫助將不勝感激。

回答

0

難道你不能鉤入posts_clauses並使用$ pieces對象來改變查詢嗎? (我想補充一點的註釋,但不具備名譽)

add_filter('posts_clauses', function($pieces, $obj) { 
    print_r($pieces); 
}); 

HM - 這可能仍然是不夠的。 post_clauses $件對象返回:

Array (
    [where] => AND (((wp_posts.post_title LIKE '%test%') OR (wp_posts.post_excerpt LIKE '%test%') OR (wp_posts.post_content LIKE '%test%'))) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') 
    [groupby] => 
    [join] => 
    [orderby] => wp_posts.post_title LIKE '%test%' DESC, wp_posts.post_date DESC 
    [distinct] => 
    [fields] => wp_posts.* 
    [limits] => LIMIT 0, 10 
) 
+0

你能舉個例子嗎?因爲我想徹底改變查詢,而不僅僅是它的一部分。 – input

+0

啊,對不起 - post_clausules會更好。查看更新的答案。 –