2012-03-14 5 views
0

저는 magic fields을 거의 독점적으로 사용하는 WordPress 사이트를 만들었습니다 (기본 게시물 등이 아닌).검색 Wordpress Magic Fields 아이템? 검색 결과가 없습니다.

그러나 검색 기능을 구현하려고 시도 중입니다. WordPress에서 Magic Fields로 만든 콘텐츠를 찾을 수 없습니다.

커스텀 WP_Query를 만들기 위해 검색을 변경했지만 아직 행운이 없습니다. 예를 들어 post_type이 'project'인 경우 :

$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

관련 문제