SQL查询包含特定的WooCommerce标记

时间:2017-03-21 06:53:39

标签: php mysql sql wordpress woocommerce

我想修改一个WooCommerce插件,以便选择名称所选标签的产品"右标记"例如。

我的代码格式如下:

$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 */
你能帮帮我吗?

非常感谢 吉安尼斯

1 个答案:

答案 0 :(得分: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查询&amp;代码经过测试并有效。

希望这有帮助!