2015-01-21 3 views
0

woocommerce 웹 사이트에서 사용자 정의 검색 쿼리를 만들었습니다. 다 잘 보입니다. 그러나 SKU 번호와 같은 것을 검색 할 때 두 개의 검색 결과가 표시됩니다.프론트 엔드에서 중복 결과를 보여주는 Wordpress 사용자 정의

예 검색 결과
예 - http://bxcell.iteratemarketing.com/?s=BE0204&post_type=product

여기에 내 CODE 미리

add_filter('the_posts', 'woo_custom_fields_query'); 

    function woo_custom_fields_query($posts, $query = false) { 
     if (is_search()){ 
     $noIds = array(0); 
      foreach($posts as $post){ 
       $noIds[] = $post->ID; 
      } 

      $mPosts = get_post_by_custom_field(get_search_query(), $noIds);  

      if ($mPosts){ 
       foreach($mPosts as $product_id){ 
        $posts[] = get_post($product_id->post_id);     
       } 
      } 
      return $posts; 
     } 
     return $posts; 
    } 

    function get_post_by_custom_field($qrval, $noIds) { 
     global $wpdb, $wp_query; 

     $pstArr = array(); 

     $noIdsQrl = implode(",", $noIds); 
     $vrvspsts = $wpdb->get_results(
         " 
      SELECT p.post_parent as post_id FROM $wpdb->posts as p 
      join $wpdb->postmeta pm 
      on p.ID = pm.post_id   
      and pm.meta_value LIKE '%$qrval%' 
      join $wpdb->postmeta visibility 
      on p.post_parent = visibility.post_id  
      and visibility.meta_key = '_visibility' 
      and visibility.meta_value <> 'hidden' 
      where 1 
      AND p.post_parent <> 0 
      and p.ID not in ($noIdsQrl) 
      and p.post_status = 'publish' 
      group by p.post_parent 
      " 
    ); 

     foreach($vrvspsts as $post){ 
      $noIds[] = $post->post_id; 
     } 

     $noIdsQrl = implode(",", $noIds); 

     $rglprds = $wpdb->get_results(
      "SELECT p.ID as post_id FROM $wpdb->posts as p 
      join $wpdb->postmeta pm 
      on p.ID = pm.post_id   
      AND pm.meta_value LIKE '%$qrval%' 
      join $wpdb->postmeta visibility 
      on p.ID = visibility.post_id  
      and visibility.meta_key = '_visibility' 
      and visibility.meta_value <> 'hidden' 
      where 1 
      and (p.post_parent = 0 or p.post_parent is null) 
      and p.ID not in ($noIdsQrl) 
      and p.post_status = 'publish' 
      group by p.ID 

    "); 

     $pstArr = array_merge($vrvspsts, $rglprds); 
     $wp_query->found_posts += sizeof($pstArr);  
     return $pstArr; 
    } 

감사합니다!

답변

1

두 가지 쿼리의 논리에 문제가 있다고 생각합니다. 병합 된 배열에서 두 개의 결과에서 중복 된 글쇠 ID를 가져 오는 중입니다.

$pstArr = array_unique(array_merge($vrvspsts, $rglprds)); 
+0

그 덕분에 완벽했습니다. 정말 고마워!!!! –

관련 문제