2012-03-14 91 views
0

我已經建立了一個幾乎完全使用magic fields的wordpress網站(而不是默認帖子等)。搜索Wordpress Magic Fields項目?沒有搜索結果

但是,我現在試圖實現搜索功能,並發現wordpress無法找到Magic Fields創建的任何內容。

我改變了我的搜索來創建一個自定義WP_Query,但我仍然沒有任何運氣。例如,我有一個post_type的'項目':

$searchValue = $_GET['s']; 

    $args = array(
     'post_type' => 'project', 
     'posts_per_page' => -1, 
     'meta_value' => $searchValue, 
     'meta_key' => 'title' 
    ); 

    $query = new WP_Query($args); 

這不會返回任何結果。我哪裏錯了?

非常感謝提前!

回答

3

我也有魔術領域和WordPress的搜索問題。標準的WordPress搜索只搜索郵箱內容。處理魔術字段內容的搜索方法是搜索後續媒體。

$add_value = true; 
$query_array = array(); 
$query_for_posts = "page_id="; 
$search_guery = $_GET['s']; 
$search_results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."postmeta WHERE meta_value LIKE '%" . $search_guery ."%' ORDER BY post_id"); 

if(!empty($search_results)) 
{ 
    foreach ($search_results as $search_result) 
    { 
     //loop through results 
     for($i=0;$i<sizeof($query_array);$i++) 
     { 
      //check if post id in the array 
      if($search_result->post_id == $query_array[$i]) 
       $add_value = false; 
     } 
     if($add_value) 
     { 
      //add the post id to the array if not a duplicate 
      array_push($query_array, $search_result->post_id); 
      //also add id for WP_Query 
      $query_for_posts .= $search_result->post_id . ","; 
     } 
     $add_value = true; 
    } 
} 

然後以顯示結果。

if(!empty($query_array)) 
{ 
    for($i=0;$i<sizeof($query_array);$i++) 
    { 
     //get post from array of ids 
     $post = get_page($query_array[$i]); 
     //make sure the post is published 
     if($post->post_status == 'publish') 
      echo '<h3><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></h3>'; 
    } 
} 
else 
{ 
    //tell the user there are no results 
} 

您還可以在WP_query中使用$ query_for_posts變量。它應該具有值page_id = 1,3,7,9,23 ...所有來自搜索結果的帖子ID。

+0

完美,謝謝! – waffl 2012-04-07 17:23:24