2017-03-21 41 views
0

例如,我想修改WooCommerce插件以選擇名稱爲「右標記」的選定標記的產品。包含特定WooCommerce標記的SQL查詢

我的代碼的形式爲:

$query =$wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "posts WHERE `post_type` LIKE 'product' AND `post_status` LIKE 'publish'",0); 
/* do something */ 
foreach ($result as $index => $prod) { 

    $sql = $wpdb->prepare( "SELECT * 
    FROM " . $wpdb->prefix . "postmeta 
    WHERE `post_id` =" . $prod->ID . " AND `meta_key` LIKE '_stock_status';",0); 
    /* do something */ 
    /* do something */ 

你能幫助我嗎?

非常感謝 Giannis

+0

您只需要'MySQL'查詢或'wp_query'也可以獲得屬於特定標籤的產品? –

+0

我只需要'MySQL',因爲我需要修改特定的插件。這是我的問題的原因 –

回答

0

MySQL查詢通過特定的標籤,以獲得產品:

SELECT posts.ID AS product_id, 
     posts.post_title AS product_title 
FROM wp_posts AS posts, 
    wp_terms AS terms, 
    wp_term_relationships AS term_relationships, 
    wp_term_taxonomy AS term_taxonomy 
WHERE term_relationships.object_id = posts.ID 
    AND term_taxonomy.term_id = terms.term_id 
    AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id 
    AND posts.post_type = 'product' 
    AND posts.post_status = 'publish' 
    AND term_taxonomy.taxonomy = 'product_tag' 
    AND terms.slug = 'my-tag-1'; -- Replace it with your product tag 


等效的WordPress/PHP查詢

global $wpdb; 
$query = "SELECT posts.ID AS product_id, 
      posts.post_title AS product_title 
    FROM {$wpdb->prefix}posts AS posts, 
      {$wpdb->prefix}terms AS terms, 
      {$wpdb->prefix}term_relationships AS term_relationships, 
      {$wpdb->prefix}term_taxonomy AS term_taxonomy 
    WHERE term_relationships.object_id = posts.ID 
     AND term_taxonomy.term_id = terms.term_id 
     AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id 
     AND posts.post_type = 'product' 
     AND posts.post_status = 'publish' 
     AND term_taxonomy.taxonomy = 'product_tag' 
     AND terms.slug = 'my-tag-1';"; //Replace it with your product tag 
$products = $wpdb->get_results($query); 

if (!empty($products)) 
{ 
    //looping through all the products 
    foreach ($products as $key => $product) 
    { 
     //... 
     $product->product_id; 
     $product->product_title; 
     //... 
    } 
} 

兩個MySQL查詢&守則測試和工作。

希望這會有所幫助!