trying to fetch products from wooocommerce to script with sql, but have no luck in it. tried that:
$query = "SELECT
p.id, p.post_title as title, pm.meta_value as price
FROM wp_posts 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
and tt.term_id = t.term_id
LEFT JOIN wp_postmeta pm
ON
p.ID = pm.post_id
and pm.meta_key = 'price'
WHERE
1
and tt.taxonomy = 'product_type'
and t.name = %s
AND post_type = 'product'
AND post_status = 'publish'
GROUP BY p.id
"
but had no product. what should i do to fetch products with sql in woocomerce?
First of all, don't use SQL queries, it's a bad practice.
You should use Wordpress built in WP_Query
Second, this question has already been answered over here : Woocommerce get products
I'll suggest to have a look there :)
And for more infos about Wordpress Queries, over here : https://developer.wordpress.org/reference/classes/wp_query/
Related
Good day,
I have a woocommerce eshop in which I do not use variants ... all products are simple. But for partner shops I need to create XML with variable products.
I have products that are not published are "hidden" (custom post status).
I also have "parent_sku" for custom field products where my parent product is specified.
I have a SQL code that extracts all the necessary products from the database and also sku and parent_sku
I would need to modify the SQL code to display the post_title of the parent product for the product variant.
I enclose my SQL code and some pictures
SELECT
wp_posts.post_title AS title,
wp_posts.post_excerpt AS shortdesc,
wp_posts.post_content AS longdesc,
wp_postmeta1.meta_value AS sku,
wp_postmeta6.meta_value AS parent_sku,
wp_postmeta2.meta_value AS price,
wp_posts1.guid AS img,
wp_postmeta5.meta_value AS mall,
GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS product_categories
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
ON wp_postmeta1.post_id = wp_posts.ID
AND wp_postmeta1.meta_key = '_sku'
LEFT JOIN wp_postmeta wp_postmeta2
ON wp_postmeta2.post_id = wp_posts.ID
AND wp_postmeta2.meta_key = '_regular_price'
LEFT JOIN wp_postmeta wp_postmeta3
ON wp_postmeta3.post_id = wp_posts.ID
AND wp_postmeta3.meta_key = '_thumbnail_id'
LEFT JOIN wp_posts wp_posts1
ON wp_posts1.ID = wp_postmeta3.meta_value
AND wp_posts1.post_type = 'attachment'
LEFT JOIN wp_postmeta wp_postmeta5
ON wp_postmeta5.post_id = wp_posts.ID
AND wp_postmeta5.meta_key = 'mall'
LEFT JOIN wp_postmeta wp_postmeta6
ON wp_postmeta6.post_id = wp_posts.ID
AND wp_postmeta6.meta_key = 'parent_sku'
LEFT JOIN wp_term_relationships
ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'product_cat'
LEFT JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'product' AND ( wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'hidden') AND wp_postmeta5.meta_value = 'yes'
GROUP BY wp_posts.ID
ORDER BY sku ASC
my code show this table
IMG_1
I need to display a spreadsheet like this
IMG_2
thank you for any help <3
My first approach would be something like this. I can not test it cause I have no sample DB
Make your sample SQL a subquery of a new query (it has parent SKU)
Join with another subquery which contains parent SKU and the data you want.
The main Query selects the fields in the desired order.
SELECT baseSQL.title p_title,
parentSQL.title AS p_parent_title, -- second field in picture
baseSQL.shortdesc AS p_shortdesc,
baseSQL.longdesc AS p_longdesc,
baseSQL.sku AS p_sku,
baseSQL.parent_sku AS p_parent_sku,
baseSQL.price AS p_price,
baseSQL.img AS p_img,
baseSQL.mall AS p_mall,
baseSQL.product_categories AS p_categories
FROM
( SELECT
wp_posts.post_title AS title,
wp_posts.post_excerpt AS shortdesc,
wp_posts.post_content AS longdesc,
wp_postmeta1.meta_value AS sku,
wp_postmeta6.meta_value AS parent_sku,
wp_postmeta2.meta_value AS price,
wp_posts1.guid AS img,
wp_postmeta5.meta_value AS mall,
GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS product_categories
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
ON wp_postmeta1.post_id = wp_posts.ID
AND wp_postmeta1.meta_key = '_sku'
LEFT JOIN wp_postmeta wp_postmeta2
ON wp_postmeta2.post_id = wp_posts.ID
AND wp_postmeta2.meta_key = '_regular_price'
LEFT JOIN wp_postmeta wp_postmeta3
ON wp_postmeta3.post_id = wp_posts.ID
AND wp_postmeta3.meta_key = '_thumbnail_id'
LEFT JOIN wp_posts wp_posts1
ON wp_posts1.ID = wp_postmeta3.meta_value
AND wp_posts1.post_type = 'attachment'
LEFT JOIN wp_postmeta wp_postmeta5
ON wp_postmeta5.post_id = wp_posts.ID
AND wp_postmeta5.meta_key = 'send'
LEFT JOIN wp_postmeta wp_postmeta6
ON wp_postmeta6.post_id = wp_posts.ID
AND wp_postmeta6.meta_key = 'parent_plu'
LEFT JOIN wp_term_relationships
ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'product_cat'
LEFT JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'product' AND ( wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'hidden') AND wp_postmeta5.meta_value = 'yes'
GROUP BY wp_posts.ID
ORDER BY sku ASC
) baseSQL
LEFT JOIN
(
SELECT
wp_posts.post_title AS title,
wp_postmeta1.meta_value AS sku
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
ON wp_postmeta1.post_id = wp_posts.ID
AND wp_postmeta1.meta_key = '_sku'
) parentSQL
ON baseSQL.parent_sku = parentSQL.sku
AND baseSQL.parent_sku > 0
GROUP BY baseSQL.sku
Check the performance of this, maybe moving grouping and ordering outside will become in a better performance.
I don't propose to make a subquery in a nested JOIN within original LEFT JOINS because I cannot test it and from here and now, I can't image how would react this "cross joining" with postmeta1 and postmeta6.
I recommend to implement some trigger and have sku and parent_sku fields in table wp_posts and you could do simple subquery with the table itself.
In mySQL there is no hierarchical queries (until I know mySQL) like in Oracle, but maybe this helps to improve your query.
It's up to you.
Sorry If this don't work on copy/paste, It's just an approach.
Edited with final functional SQL
my fully functional code
SELECT baseSQL.title p_title,
parentSQL.title AS p_parent_title, -- second field in picture
baseSQL.shortdesc AS p_shortdesc,
baseSQL.longdesc AS p_longdesc,
baseSQL.sku AS p_sku,
baseSQL.parent_sku AS p_parent_sku,
baseSQL.price AS p_price,
baseSQL.img AS p_img,
baseSQL.mall AS p_mall,
baseSQL.product_categories AS p_categories
FROM
( SELECT
wp_posts.post_title AS title,
wp_posts.post_excerpt AS shortdesc,
wp_posts.post_content AS longdesc,
wp_postmeta1.meta_value AS sku,
wp_postmeta6.meta_value AS parent_sku,
wp_postmeta2.meta_value AS price,
wp_posts1.guid AS img,
wp_postmeta5.meta_value AS mall,
GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS product_categories
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
ON wp_postmeta1.post_id = wp_posts.ID
AND wp_postmeta1.meta_key = '_sku'
LEFT JOIN wp_postmeta wp_postmeta2
ON wp_postmeta2.post_id = wp_posts.ID
AND wp_postmeta2.meta_key = '_regular_price'
LEFT JOIN wp_postmeta wp_postmeta3
ON wp_postmeta3.post_id = wp_posts.ID
AND wp_postmeta3.meta_key = '_thumbnail_id'
LEFT JOIN wp_posts wp_posts1
ON wp_posts1.ID = wp_postmeta3.meta_value
AND wp_posts1.post_type = 'attachment'
LEFT JOIN wp_postmeta wp_postmeta5
ON wp_postmeta5.post_id = wp_posts.ID
AND wp_postmeta5.meta_key = 'send'
LEFT JOIN wp_postmeta wp_postmeta6
ON wp_postmeta6.post_id = wp_posts.ID
AND wp_postmeta6.meta_key = 'parent_plu'
LEFT JOIN wp_term_relationships
ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = 'product_cat'
LEFT JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'product' AND ( wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'hidden') AND wp_postmeta5.meta_value = 'yes'
GROUP BY wp_posts.ID
ORDER BY sku ASC
) baseSQL
LEFT JOIN
(
SELECT
wp_posts.post_title AS title,
wp_postmeta1.meta_value AS sku
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
ON wp_postmeta1.post_id = wp_posts.ID
AND wp_postmeta1.meta_key = '_sku'
) parentSQL
ON baseSQL.parent_sku = parentSQL.sku
AND baseSQL.parent_sku > 0
GROUP BY baseSQL.sku
I need to append something to post content if posts are in a certain category.
Here's the code I have to show all the posts in the category.
SELECT * FROM wp_posts
LEFT JOIN wp_term_relationships ON
(wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON
(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_posts.post_status = 'publish'
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id = 1
ORDER BY post_date DESC
I need to write an update query to append post content. Here's that:
UPDATE Table SET Field=CONCAT(Field,'your extra html');
How do I write an update query with the results from the select query?
It may help you:
UPDATE wp SET Field=CONCAT(Field,'your extra html')
WHERE wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) AND wp_posts.post_status = 'publish'
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id = 1
ORDER BY post_date DESC
I need to select the last post from 3 different categories.
Till now, I've got a query like this:
select ID, t.term_id term_id, post_title, post_content, t.slug as category
from e_posts p
inner join e_term_relationships tr on p.ID = tr.`object_id`
inner join e_term_taxonomy tt on tr.term_taxonomy_id = tt.term_taxonomy_id
inner join e_terms t on t.term_id = tt.term_id
where tt.taxonomy = "category" and t.term_id IN(4,670,158)
and the result is like this:
If I try to group by term_id, I get the first post from each term (category). Even if I order by date. What can I do? I guess it's not too hard but can't figure out..
You problem relates to greatest-n-per-group tag (take a look) you need to get the most recent posts where your criteria matches. So here is your solution: get the posts with their max date self join by using p.ID = tr.object_idAND p.post_date = tr.post_date so it takes care to get the recent posts
SELECT DISTINCT
p.ID,
t.term_id term_id,
p.post_title,
p.post_content,
t.slug AS category
FROM
e_posts p
INNER JOIN
(SELECT
etr.*,
pp.ID,
MAX(pp.post_date) post_date
FROM
e_posts pp
INNER JOIN
e_term_relationships etr
ON
pp.ID = etr .`object_id`
GROUP BY
pp.ID,etr.term_taxonomy_id) tr
ON
(p.ID = tr.`object_id` AND p.post_date = tr.post_date)
INNER JOIN
e_term_taxonomy tt
ON
tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN
e_terms t
ON
t.term_id = tt.term_id
WHERE
tt.taxonomy = "category" AND
t.term_id IN(4,670,158)
I'm using the following sql query to get a custom menu by it's name:
SELECT p2.post_title, p2.post_name, p2.guid
FROM wp_posts p1
INNER JOIN wp_term_relationships AS TR
ON TR.object_id = p1.ID
INNER JOIN wp_postmeta AS PM
ON pm.post_id = p1.ID
INNER JOIN wp_posts AS p2
ON p2.ID = PM.meta_value
WHERE p1.post_type = 'nav_menu_item'
AND TR.term_taxonomy_id = ( SELECT wp_terms.term_id FROM wp_terms WHERE wp_terms.slug = '$custom_menu')
AND pm.meta_key = '_menu_item_object_id'
ORDER BY p1.menu_order ASC
Now I'm using WPML on the site and need to get this menu by language code. How do I add it to this query?
Thanks in advance for any help with this!
Try this, works for me.
SELECT p2.ID, p2.post_content, p2.post_title, p2.post_name, p2.guid, p1.post_parent
FROM wp_posts p1
INNER JOIN wp_term_relationships AS TR
ON TR.object_id = p1.ID
INNER JOIN wp_postmeta AS pm
ON pm.post_id = p1.ID
INNER JOIN wp_posts AS p2
ON p2.ID = pm.meta_value
WHERE p1.post_type = 'nav_menu_item'
AND TR.term_taxonomy_id = ( SELECT wp_term_taxonomy.term_taxonomy_id FROM wp_terms LEFT JOIN wp_term_taxonomy ON wp_terms.term_id=wp_term_taxonomy.term_id AND `taxonomy` = 'nav_menu' WHERE wp_terms.slug = '" . $this->real_escape_string($slug) . "')
AND pm.meta_key = '_menu_item_object_id'
ORDER BY p1.menu_order ASC
I am using WP Data Tables to create a table from the SQL DB. In the wordpress backend the example code to use looked like this:
SELECT post_id, post_date
FROM wp_posts
WHERE post_type = 'custom_post_type'
AND post_status = 'publish'
Im trying to get custom field values from the post meta. Here is what I have so far...
SELECT post_id, post_date
FROM wp_posts
WHERE post_type = 'custom_post_type'
AND post_status = 'publish'
AND SELECT custom_field_key_1, custom_field_key_2, custom_field_key_3
FROM wp_postmeta
WHERE post_id = post_id
UPDATE:
I found that p.ID was needed instead of post_id and that I need search for the meta_key. Something like...
SELECT p.post_title,
p.post_date,
pm.meta_key = 'custom_field_key'
FROM wp_posts p
INNER JOIN wp_postmeta pm
ON p.ID = pm.post_id
WHERE p.post_type = 'custom_post_type'
AND p.post_status = 'publish'
Use an INNER JOIN:
SELECT p.post_id,
p.post_date,
pm.custom_field_key_1,
pm.custom_field_key_2,
pm.custom_field_key_3
FROM wp_posts p
INNER JOIN wp_postmeta pm
ON p.post_id = pm.post_id
WHERE p.post_type = 'custom_post_type'
AND p.post_status = 'publish'
Assuming that standard SQL is supported you will need something like this (untested):
SELECT w.post_id, w.post_date, m.custom_field_key_1, m.custom_field_key_2, m.custom_field_key_3
FROM wp_posts w, wp_postmeta m
WHERE post_type = 'custom_post_type' AND post_status = 'publish'
AND w.post.id = m.post.id
You can try this.
SELECT p.post_id, p.post_date,
pm.custom_field_key_1, pm.custom_field_key_2, pm.custom_field_key_3
FROM wp_posts p
JOIN wp_postmeta pm ON p.post_id = pm.post_id
WHERE p.post_type = 'custom_post_type'
AND p.post_status = 'publish'
Ok even though you updated an answer. I took your example and I worked this out:
SELECT p.ID,
p.post_title,
pm.meta_value as 'value1',
pma.meta_value as 'value2'
FROM wp_posts p
INNER JOIN wp_postmeta AS pm ON pm.post_id = p.ID
INNER JOIN wp_postmeta AS pma ON pma.post_id = p.ID
WHERE
pma.meta_key = 'custom_field_key_1' AND
pm.meta_key = 'custom_field_key_2' AND
p.post_type = 'your_post_type' AND
p.post_status = 'publish'
So here I am using alias AS for values hosted in the same table, INNER JOIN for wp_postmeta and post_id and that's all.
References:
Multiple Inner join same table
By doing this you will get an array with posts and your selected custom fields:
array(1) {
[0]=> object(stdClass)#341 (4) {
["ID"]=> string(1) "1123"
["post_title"]=> string(15) "Your post title"
["custom_field_key_1"]=> string(12) "Your value 1 "
["custom_field_key_2"]=> string(29) "Your value 2"
}
You can add as many alias and meta_key as you need. Hope this help!