SQL query for custom menu Wordpress using WPML - sql

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

Related

how to get SQL woocommerce products?

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/

MySQL DELETE post_content from all posts except specific category

I need a SQL query that update post_content column from all posts except this category ID = 2365
I tried many codes but I still can not find the solution
UPDATE `wp_posts` SET post_content='' WHERE 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_term_taxonomy.term_id NOT IN (2365)
You can try below query:
UPDATE 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
set wp_posts=''
WHERE wp_posts.id not in (
select wp_posts.id from wp_posts
inner join wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
inner join wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE wp_term_taxonomy.term_id =2365)

SQL Query for getting woocommerce products details in csv file

I want to get the woocommerce product attribute pa_model and other data which is from wp_posts and wp_postmeta tables but here I am concern only with pa_model as following query showing me the null in model column?
select DISTINCT wp_posts.ID,post_title, rpr.meta_value as regular_price , pr.meta_value as price, post_excerpt,post_content
, imgpm.meta_value as image, upcpm.meta_value as upc, wpm.meta_value as weight, shpm.meta_value as free_ship, pcppm.meta_value as pricecost,
wu.display_name as vendor, pm.model
from wp_posts
left join wp_postmeta rpr on wp_posts.ID = rpr.post_id and rpr.meta_key='_regular_price'
left join wp_postmeta pr on wp_posts.ID = pr.post_id and pr.meta_key='_price'
left join wp_postmeta imgpm on wp_posts.ID = imgpm.post_id and imgpm.meta_key='_wp_attached_file'
left join wp_postmeta upcpm on wp_posts.ID = upcpm.post_id and upcpm.meta_key='upc'
left join wp_postmeta wpm on wp_posts.ID = wpm.post_id and wpm.meta_key='_weight'
left join wp_postmeta shpm on wp_posts.ID = shpm.post_id and shpm.meta_key='free_ship'
left join wp_postmeta pcppm on wp_posts.ID = pcppm.post_id and pcppm.meta_key='_product_cost_price'
left join wp_users wu on wp_posts.post_author = wu.ID
left join (select GROUP_CONCAT(name) as model,object_id FROM wp_terms
join wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id and wp_term_taxonomy.taxonomy = 'pa_model'
join wp_term_relationships wtr on wtr.term_taxonomy_id=wp_term_taxonomy.term_taxonomy_id GROUP by object_id) as pm on pm.object_id=wp_posts.id
where wp_posts.post_type='product' limit 0,100
In this query for checking purpose I have limited my product with limit 100.
I just have tested your query and it works fine for me and select column model perfectly. You should check wp_term_taxonomy.taxonomy = 'pa_model'. Do you certain that 'pa_model' exactly exists in wp_term_taxonomy and has terms in wp_terms?

SQL Display the parent title of the product with the child title

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

sql subquery returns more than on 1 row error

SELECT wp_terms.name AS 'Exam',
(SELECT wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key in ('_order_total') ),
wp.id,
wp_woocommerce_order_itemmeta.meta_value AS 'Product Id',
f.order_item_name,
(SELECT wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key in ('_billing_email'))
FROM wp_term_taxonomy
INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_woocommerce_order_itemmeta ON wp_woocommerce_order_itemmeta.meta_value = wp_term_relationships.object_id
INNER JOIN (SELECT f.order_item_id,
f.order_id,
f.order_item_name
FROM wp_woocommerce_order_items f) f ON f.order_item_id = wp_woocommerce_order_itemmeta.order_item_id
INNER JOIN wp_postmeta ON wp_postmeta.post_id = f.order_id
INNER JOIN (SELECT wp.id,
FROM wp_posts wp
INNER JOIN wp_postmeta wm ON wp.id = wm.post_id
WHERE wp.post_type = 'shop_order'
AND wp.post_status = 'wc-completed' AND wm.meta_key = '_completed_date') wp ON wp.id = wp_postmeta.post_id
WHERE wp_term_taxonomy.taxonomy = 'product_cat'
AND wp_term_taxonomy.term_id IN (2825,2829,2833,2837,2844,2851,2847)
AND wp_woocommerce_order_itemmeta.meta_key = '_product_id'
AND wp_postmeta.meta_key IN ('_order_total','_billing_email')
The above sql query throws subquery returns more than one row error. I want billing address in column and order total in another column. How to do that?
As vims said, you've more than one row in the 2 subselects from wp_postmeta.
In fact you don't join them by an id or something like that and the following select, for example
SELECT wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key in ('_order_total')
will return all rows in the DB with key ='_order_total'
The same holds for
SELECT wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key in ('_billing_email'))
So, you should add a join clause on some id, to both of them.
I'm sorry I'm not expert on WP and\or WooCommerce, so I can0't give you an hint on which field use.
Moreover you should put those subselects in the FROM and WHERE part or your select. A subselect in the selectlist is not a good practice.
The subqueries are not limited to the post_id in question. Hence, you are getting the order_totals for all the posts. Add something like this to each subquery:
AND f.order_id = post_id
SELECT
wp_terms.name AS 'Exam',
(
SELECT wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key in ('_order_total') and rownum=1
),
wp.id,
wp_woocommerce_order_itemmeta.meta_value AS 'Product Id',
f.order_item_name,
(
SELECT wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key in ('_billing_email') and rownum=1
)
FROM
wp_term_taxonomy
....