包含特定 WooCommerce 标签的 SQL 查询 [英] SQL query to include specific WooCommerce tag
本文介绍了包含特定 WooCommerce 标签的 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想修改一个 WooCommerce 插件,以便选择名称为right-tag"的所选标签的产品.
I want to modify a WooCommerce plugin in order to pick products of a selected tag of the name "right-tag" for example.
我的代码是这样的:
$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 查询以按特定标签获取产品:
MySQL query to get product by specific tag:
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 查询
Equivalent WordPress/PHP query
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 查询和代码已经过测试并且可以正常工作.
希望这会有所帮助!
这篇关于包含特定 WooCommerce 标签的 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文