10年后,但总比没有好。。。
类别和标记不相互关联。它们都与岗位/产品相关。因此,您需要查询所有帖子/产品并获取它们的ID,这样您就可以查询产品/帖子ID在这组ID中的标记。
有关WordPress帖子,请参见以下查询:
SET @CategoryByID = 1; -- set correct category ID
-- SET @CategoryByName = \'Politics\'; -- set correct category name
SELECT DISTINCT t.* FROM wp_posts AS p
LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
WHERE p.post_type="post" -- <<< for wordpress post type
AND p.post_status = \'publish\'
AND tt.taxonomy = "post_tag" -- <<< for wordpress post tags
AND p.ID IN
(SELECT p.ID FROM wp_posts AS p
LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
WHERE p.post_type="post" <<< for wordpress post type
AND p.post_status = \'publish\'
AND tt.taxonomy = "category" -- <<< for wordpress post category
AND tt.term_taxonomy_id = @CategoryByID -- <<< search category by id (use one)
-- AND t.name = @CategoryByName -- <<< search category by name (use one)
ORDER BY p.ID)
ORDER BY p.ID
请参见下面的WooCommerce产品查询示例:
SET @CategoryByID = 18; -- set correct category ID
-- SET @CategoryByName = \'T-SHIRT\'; -- set correct category name
SELECT DISTINCT t.* FROM wp_posts AS p
LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
WHERE p.post_type="product" -- <<< for woocommerce post type
AND p.post_status = \'publish\'
AND tt.taxonomy = "product_tag" -- <<< for woocommerce tags
AND p.ID IN
(SELECT p.ID FROM wp_posts AS p
LEFT JOIN wp_term_relationships tr ON p.ID = tr.object_id
LEFT JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms t ON t.term_id = tt.term_id
WHERE p.post_type="product"
AND p.post_status = \'publish\'
AND tt.taxonomy = "product_cat" -- <<< for woocommerce products
AND tt.term_taxonomy_id = @CategoryByID -- <<< search category by id (use one)
-- AND t.name = @CategoryByName -- <<< search category by name (use one)
ORDER BY p.ID)
ORDER BY p.ID