2012-03-14 49 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