2015-01-28 6 views
0

모든 제품 추가 기능의 변형 (견본)을 나열하기 위해 MySQL 쿼리를 사용하고 있습니다.이 제품을 변형 할 수 있으며 유사 제품과 관련된 제품을 나열 할 수 있습니다. 내가 가진 문제는 제품 카테고리 ID도 나열하는 것입니다.해당 범주 ID가있는 Woocommerce 제품 목록 - MySQL/PHP

저는 MySQL에 대한 공식 교육이 없으므로 성능에 대한 조언과 조언도 큰 가치가 있습니다!

여기에 addon 변형 및 해당 제품의 목록을 얻으려는 코드가 있습니다. 해당 제품의 카테고리 ID도 가져와야합니다.

$prod_attributes_sql = 'SELECT DISTINCT wt.slug AS term_slug, wtr.term_taxonomy_id, wpp.post_title AS wpost_title, wt.term_id AS wpt_terms, cpt.term_id AS cpt_terms 
    FROM ' 
. " {$wc_price_table->cat_price_table_name} cpt" 
. ' RIGHT OUTER JOIN wp_terms wt ON cpt.term_id=wt.term_id' 
. ' INNER JOIN wp_term_relationships wtr ON wt.term_id = wtr.term_taxonomy_id' 
. ' INNER JOIN wp_posts wpp ON wtr.object_id = wpp.ID' 
. ' RIGHT OUTER JOIN wp_term_taxonomy ttx on wt.term_id = ttx.term_id' 
. ' WHERE ttx.taxonomy IN (\'' . implode("','", $slugsnews) . '\')' 
. ' ORDER BY wt.name'; 
$prod_attributes = $wpdb->get_results($prod_attributes_sql) or die(mysql_error()); 

addon 변형 목록과 관련 제품이 출력됩니다. 그러나 그 제품의 카테고리 ID를 어떻게 포함 할까?

추가 설명이나 코드를 추가해야하는지 알려주세요.

미리 감사드립니다.

 $term_id = array(); 
     foreach ($product_categories as $key => $category) { 
      foreach ($category['terms'] as $term) { 
       $term_id[] = $term->term_id; 
      } 
     } 

하고이 같은 내 SQL에 포함 :

 $prod_attributes_sql = 'SELECT DISTINCT wt.slug AS term_slug, wt.name, ttx.taxonomy, wtr.term_taxonomy_id, wpp.post_title AS wpost_title, wt.term_id AS wpt_terms, cpt.term_id AS cpt_terms 
      FROM ' 
     . " {$wc_price_table->cat_price_table_name} cpt" 
     . ' RIGHT OUTER JOIN wp_terms wt ON cpt.term_id=wt.term_id' 
     . ' INNER JOIN wp_term_relationships wtr ON wt.term_id = wtr.term_taxonomy_id' 
     . ' INNER JOIN wp_posts wpp ON wtr.object_id = wpp.ID' 
     . ' RIGHT OUTER JOIN wp_term_taxonomy ttx on wt.term_id = ttx.term_id' 
     . ' WHERE cpt.term_id IN(' . implode(',', $term_id) . ')' 
     . ' OR ttx.taxonomy IN (\'' . implode("','", $slugsnews) . '\')' 
     . ' OR ttx.term_taxonomy_id = cpt.term_id' 
     . ' ORDER BY wpp.post_title'; 
     $prod_attributes = $wpdb->get_results($prod_attributes_sql) or die(mysql_error()); 

답변

0

나는 조건을 얻기 위해 다음 foreach를 사용하여 $term_id을 포함하는 쿼리를 변경할 필요 끝났다 공장.

관련 문제