【问题标题】:SQL query to include specific WooCommerce tag包含特定 WooCommerce 标记的 SQL 查询
【发布时间】:2017-03-21 06:53:38
【问题描述】:

我想修改一个 WooCommerce 插件,以便选择名称为“right-tag”的选定标签的产品。

我的代码格式为:

$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 */

你能帮帮我吗?

非常感谢 扬尼斯

【问题讨论】:

  • 您只需要MySQL 查询或wp_query 也可以获取属于特定标签的产品?
  • 我只需要MySQL,因为我需要修改一个特定的插件。这是我的问题的原因

标签: php mysql sql wordpress woocommerce


【解决方案1】:

通过特定标签获取产品的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 查询和代码都经过测试并且可以正常工作。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2018-04-18
    相关资源
    最近更新 更多