Answer the question
In order to leave comments, you need to log in
How to get products and categories from WP DB that have parent categories?
Hello. Faced a problem, it is impossible to get the necessary goods from a DB. I do a site search,
the data is stored in the database:
`wp_posts` - information about products
Напр., 'ID' => '8719' ,' post_title' => 'Кеды 1', 'post_type' = 'product'
Напр., 'object_id' => '8719','term_taxonomy_id' => '273'
Напр., 'term_taxonomy_id' => '273','term_id' => '273','taxonomy' => 'product_cat','parent' => '272'
'term_taxonomy_id' => '273','term_id' => '273','taxonomy' => 'product_cat','parent' => '272'
'term_taxonomy_id' => '272','term_id' => '272','taxonomy' => 'product_cat','parent' => '160'
'term_taxonomy_id' => '160','term_id' => '160','taxonomy' => 'product_cat','parent' => '0'
'term_id' => '273','name' => 'Категория1','slug' => 'kategory1'
'term_id' => '272','name' => 'Категория2','slug' => 'kategory2'
'term_id' => '160','name' => 'Категория3','slug' => 'kategory3'
DELIMITER //
CREATE FUNCTION br_get_parent_caterory_proc(var1 INT) RETURNS CHAR(100)
DETERMINISTIC
SQL SECURITY DEFINER
COMMENT 'Get parent category procedure'
BEGIN
DECLARE parent_id INT;
DECLARE result, result_while, parent_id_vchar CHAR(100);
SET parent_id = 0;
SET parent_id_vchar = '';
SET result_while = '';
SET result = var1;
SET parent_id = (SELECT `parent` FROM wp_term_taxonomy WHERE `term_taxonomy_id` = var1);
SET result = concat(result, ", ", parent_id);
WHILE parent_id <> 0 DO
SET parent_id = ( SELECT `parent` FROM wp_term_taxonomy WHERE `parent` <> 0 AND `term_taxonomy_id` = parent_id );
IF (parent_id IS NOT NULL) THEN
SET parent_id_vchar = CONVERT(parent_id, CHAR(100));
SET result_while = concat(result_while, ", ", parent_id_vchar);
END IF;
END WHILE;
SET result = concat(result, result_while);
RETURN result;
END//
DELIMITER ;
SELECT br_get_parent_caterory_proc(273);
Выводит: 272, 160 (или можно сделать полный список 273, 272, 160)
SELECT *
FROM (((`wp_posts` p
INNER JOIN `wp_term_relationships` tr ON p.ID = tr.object_id)
INNER JOIN `wp_term_taxonomy` tt ON tt.term_taxonomy_id = tr.term_taxonomy_id)
INNER JOIN `wp_terms` t ON t.term_id IN (SELECT br_get_parent_caterory_proc(t.term_id))) -- = tt.term_id)
WHERE post_type = 'product' AND taxonomy = 'product_cat' AND name LIKE '%Категория3%'
Answer the question
In order to leave comments, you need to log in
Here is the solution:
Firstly, the function returns a string, and the string is not a list (more Why does the function return the value incorrectly in the WHERE condition? )
Secondly, I rewrote the query like this:
SELECT *
FROM (((`wp_posts` p
INNER JOIN `wp_term_relationships` tr ON p.ID = tr.object_id)
INNER JOIN `wp_term_taxonomy` tt ON tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN `wp_terms` t ON FIND_IN_SET(t.term_id, br_get_parent_cat(tt.term_id)))
WHERE post_type = 'product' AND taxonomy = 'product_cat' AND t.name LIKE '%Категория3%'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question