2013-06-02 57 views
0

我需要多個複選框搜索工作,我卡住了。表單是確定的,但我不知道如何執行查詢。有人請幫助我?多個複選框搜索wordpress

形式:

<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="Loft"><div class="lbl">Loft</div> 
<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="Studio"><div class="lbl">Studio</div> 
<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="2 pieces"><div class="lbl">2 pièces</div> 
<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="3 pieces"><div class="lbl">3 pièces</div> 
<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="4 pieces"><div class="lbl">4 pièces</div> 
<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="5 pieces"><div class="lbl">5 pièces</div> 
<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="6 pieces et +"><div class="lbl">6 pièces et +</div> 
<input id="propertytype" class="noborder" type="checkbox" name="propertytype2[]" value="Proprietes, Hotels particuliers"><div class="lbl">Propriétés, Hôtels particuliers</div> 

查詢:

$search_propertytype = ""; 
if (isset($_POST['propertytype2'])) { 
    $search_propertytype = trim($_POST['propertytype2']); 
} 


if (get_option('wp_search_propertytype') == "Yes") {  
    if($search_propertytype != '') 
    { 
     $search_propertytype = trim($search_propertytype); 
     $query ="SELECT p.* FROM $wpdb->posts p, $wpdb->postmeta p1 WHERE p.ID = p1.post_id AND (p1.meta_key='propertytype_value' AND p1.meta_value='$search_propertytype' OR p1.meta_key='propertytype2_value' AND p1.meta_value='$search_propertytype')"; 
     $sptt = getIds($query); 
     $_ids = (!empty($sptt) ? (!empty($_ids) ? array_intersect($_ids, $sptt) : "") : ""); 
    } 
} 
+0

注意這裏的SQL注入。 –

+0

通過添加以下內容:$ propertytype2 = mysql_real_escape_string($ propertytype2); ? 關於如何使多個複選框搜索工作的任何想法? –

+0

使用預處理語句/ PDO。 –

回答

0

我做的WP_Query和自定義元盒子的解決方案。讓我的查詢參數如下:

$args = array(
'post_type' => 'product', 
'meta_query' => array( 
    'relation' => 'OR', 
    array(
     'key' => 'color', 
     'value' => 'blue', 
     'compare' => 'NOT LIKE' 
    ), 
    array(
     'key' => 'price', 
     'value' => array(20, 100), 
     'type' => 'numeric', 
     'compare' => 'BETWEEN' 
    ) 
) 
); 
$query = new WP_Query($args); 

檢查WordPress的正式文件:http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

您可以用這種方式進行檢查。可能會有幫助。謝謝。