2014-10-17 4 views
0

나는 WordPress 데이터베이스와 작동하는 웹 응용 프로그램을 개발하고 있습니다. 내 웹 사이트 (WooCommerce 및 WPML과 함께 작동 함)에는 영어 (1 위), 세르비아어 (2 위)의 제품이 있습니다. 필자는 데이터베이스에서 정규 및 판매 가격 등으로 제품을 가져 오는 SQL 코드를 작성했지만 Lang = en (영어 버전의 제품) 만 제공하고 싶습니다. 문제는 그 (것)들을 얻는 방법을 모른다이다.영어 버전의 제품을 모두 - WooCommerce & WPML

간단한 SQL은 :

// SELECT 
public function select (
      $table_1 = ' wp_posts ', 
      $table_2 = ' wp_postmeta ', 
      $rows = ' t1.id, t1.post_title, guid, post_type ', 
      $where = ' t1.post_status = "publish" AND t1.post_type = "product" OR t1.post_type = "product_variation" ', 
      $groupby = ' t1.ID, t1.post_title ' 
     ) { 

    // Connect to database and set charset 
    $this->connect(); 
    $this->conn->set_charset('utf8'); 


    // Published products 
    $sql = "SELECT $rows, 
      max(case when meta_key = '_regular_price' then t2.meta_value end) AS price, 
      max(case when meta_key = '_sale_price' then t2.meta_value end) AS sale, 
      max(case when meta_key = 'attribute_colors' then t2.meta_value end) AS colors, 
      max(case when meta_key = 'attribute_number' then t2.meta_value end) 
      FROM $table_1 AS t1 
      INNER JOIN $table_2 AS t2 ON (t1.ID = t2.post_id) 
      WHERE $where 
      GROUP BY $groupby"; 

    $result = mysqli_query($this->conn, $sql); 
    $published_products_count = mysqli_num_rows($result); 



    // Trashed products 
    $trashed_sql = "SELECT post_status FROM `wp_posts` 
        WHERE post_status = 'trash'"; 

    $trashed_result = mysqli_query($this->conn, $trashed_sql); 
    $trashed_products_count = mysqli_num_rows($trashed_result); 


    // If results -> show them 
    if ($result) { 

     printf("\t\t" . "<p>There are <strong>%d published</strong> products and <strong>%d trashed</strong>.</p>", $published_products_count, $trashed_products_count); 

    $table = ' 
    <table class="pure-table pure-table-bordered"> 
     <thead> 
      <tr> 
       <th>Row. №</th> 
       <th>ID</th> 
       <th>Product</th> 
       <th>Regular price</th> 
       <th>Sale price</th> 
       <th>Type</th> 
       <th>Edit</th> 
      </tr> 
     </thead>'; 

     $row_number = 1; 

     while ($row = $result->fetch_assoc()) { 

      $table .= ' 
      <tr> 
       <td>' . $row_number++ . '</td> 
       <td>' . $row["id"] . '</td> 
       <td><a href="http://www.enroll.rs/wp-admin/post.php?post=' . $row["id"] . '&action=edit" target="_blank">' . $row["post_title"] . '</a></td> 
       <td class="price">' . $row["price"] . '</td> 
       <td class="sale">' . $row["sale"] . '</td> 
       <td>' . $row["post_type"] . '</td> 
       <td><a href="edit_form.php?id=' . $row["id"] . '">Edit</a></td> 
      </tr>'; 
     } 

    $table .= ' 
    </table>'; 

     echo $table; 
    } 

    // If no results 
    else { 
     echo 'There isn't any products'; 
    } 
} 

나는 누군가가 나를 도울 희망 :

$sql = 'SELECT * from `wp_posts` WHERE post_type = product'; 


이 데이터베이스에서 선택하는 방법입니다! 사전에

감사합니다! :)

P.S. 응용 프로그램은 WordPress에 기반하지 않습니다!

+0

$ sql = 'wp_posts'에서 SELECT * WHERE post_type = product'AND language = 'en'; 또는 샘플 출력과 테이블 구조를 보지 않고'wp_posts' WHERE language IN ('en')에서 $ sql = 'SELECT *를 시도해 볼 수도 있습니다. – Haris

+0

아니요, wp_posts 테이블에 'language'라는 이름의 열이 없기 때문에 언어 = 'en'으로 시도한 적이 없습니다. – tweb

+0

당신은 테이블 구조를 보여줄 수 있습니까? – Haris

답변

1

WPML 플러그인은 표준 WP 포스트 테이블에 영향을 미치지 않으면 서 다중 노드를 관리하기 위해 몇 개의 새로운 테이블을 생성합니다. 특히 wp_icl_translations 테이블을 만들고 여기에 게시물과 언어 간의 관계가 저장됩니다.

당신은 쿼리에 두 개의 새로운 가입 문을 추가해야합니다

AND t.language_code='en' 

희망이 도움이 :

JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type = 'post_product' JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1 

그런 다음 이 같은 문이 어디에 새로운 조건을 추가 할 수 있습니다 . 감사합니다.

관련 문제