I have the following SQL code that works badly because when I try to filter by product category it displays empty records:
SQL code:
SELECT
wp_posts.ID,
CONCAT( '<a href="', wp_posts.guid, '">', wp_posts.post_title, '</a>' ) AS nazwa,
product_taxonomy_product_cat_tbl.NAME AS kategoria,
product_taxonomy_pa_j_m_tbl.NAME AS jednostka,
product_quant.meta_value AS ilość,
product_price.meta_value AS cena,
product_weight.meta_value AS waga,
product_sku.meta_value AS sku
FROM
wp_posts
LEFT JOIN wp_postmeta product_quant ON product_quant.post_id = wp_posts.ID
AND product_quant.meta_key = "_stock"
LEFT JOIN wp_postmeta product_price ON product_price.post_id = wp_posts.ID
AND product_price.meta_key = "_price"
LEFT JOIN wp_postmeta product_weight ON product_weight.post_id = wp_posts.ID
AND product_weight.meta_key = "_weight"
LEFT JOIN wp_postmeta product_sku ON product_sku.post_id = wp_posts.ID
AND product_sku.meta_key = "_sku"
INNER JOIN (
SELECT NAME
,
object_id AS id
FROM
wp_terms AS product_taxonomy_product_cat_tbl_terms
INNER JOIN wp_term_taxonomy AS product_taxonomy_product_cat_tbl_termtaxonomy ON product_taxonomy_product_cat_tbl_termtaxonomy.term_id = product_taxonomy_product_cat_tbl_terms.term_id
AND product_taxonomy_product_cat_tbl_termtaxonomy.taxonomy = 'product_cat'
INNER JOIN wp_term_relationships AS rel_product_taxonomy_product_cat_tbl ON product_taxonomy_product_cat_tbl_termtaxonomy.term_taxonomy_id = rel_product_taxonomy_product_cat_tbl.term_taxonomy_id
) AS product_taxonomy_product_cat_tbl ON product_taxonomy_product_cat_tbl.ID = wp_posts.id
INNER JOIN (
SELECT NAME
,
object_id AS id
FROM
wp_terms AS product_taxonomy_pa_j_m_tbl_terms
INNER JOIN wp_term_taxonomy AS product_taxonomy_pa_j_m_tbl_termtaxonomy ON product_taxonomy_pa_j_m_tbl_termtaxonomy.term_id = product_taxonomy_pa_j_m_tbl_terms.term_id
AND product_taxonomy_pa_j_m_tbl_termtaxonomy.taxonomy = 'pa_j-m'
INNER JOIN wp_term_relationships AS rel_product_taxonomy_pa_j_m_tbl ON product_taxonomy_pa_j_m_tbl_termtaxonomy.term_taxonomy_id = rel_product_taxonomy_pa_j_m_tbl.term_taxonomy_id
) AS product_taxonomy_pa_j_m_tbl ON product_taxonomy_pa_j_m_tbl.ID = wp_posts.id
WHERE
1 = 1
AND wp_posts.post_type = 'product'
How to improve the code so that filtering by product category works?
The operation of the code is shown on the website https://server515851.nazwa.pl/wordpress/wpn_hutniczy/o-magazynie/
The idea is to filter by categories in the tree on the left side of the page.

所有评论(0)