Wordpress sql query not working as expected - php

I have term parent called 'product_parent' that has three childs terms :
child_term_1 (id=2) (5 posts)
child_term_2 (id=3) (3 posts)
child_term_3 (id=4) (10 posts)
each post has **meta_key price ** (string) and i have to filter posts by price:
0 to 50, 50 to 100 and 100 +
this query works well (i can get 15 products):
SELECT DISTINCT p.ID FROM wp_posts as p
LEFT JOIN wp_postmeta as p ON price.post_id = p.ID
LEFT JOIN wp_relationships as t ON t.object_id = p.ID
WHERE p.post_type = 'product' AND post_status = 'publish'
AND t.term_taxonomy_id IN ('2', '3', '4')
AND p.meta_key = 'price'
AND p.meta_value LIKE '%50-100%'
OR p.meta_value LIKE '%100+%'
GROUP BY p.ID
but if i add one filter :
SELECT DISTINCT p.ID FROM wp_posts as p
LEFT JOIN wp_postmeta as p ON price.post_id = p.ID
LEFT JOIN wp_relationships as t ON t.object_id = p.ID
WHERE p.post_type = 'product' AND post_status = 'publish'
AND t.term_taxonomy_id IN ('2', '3', '4')
AND p.meta_key = 'price'
AND p.meta_value LIKE '%0-50%'
OR p.meta_value LIKE '%50-100%'
OR p.meta_value LIKE '%100+%'
GROUP BY p.ID
This returns "many posts" that not in term_taxonomy_id specified.
Someone could clarify me please? i don't want to use Wp_Query(), i just want to use sql.
Thanks for your help.

you use the same alias
then duplicate alias
try:
SELECT DISTINCT p.ID FROM wp_posts as p
LEFT JOIN wp_postmeta as pm ON pm.post_id = p.ID
LEFT JOIN wp_relationships as r ON r.object_id = p.ID
WHERE p.post_type = 'product' AND p.post_status = 'publish'
AND r.term_taxonomy_id IN ('2', '3', '4')
AND pm.meta_key = 'price'
AND pm.meta_value LIKE '%0-50%'
OR pm.meta_value LIKE '%50-100%'
OR pm.meta_value LIKE '%100+%'
GROUP BY p.ID

Related

Remove duplicate items from Woocommerce

I had problem during importing products. And now some of the products are duplicated. All of the duplicated products have the same title and attribute (collection_id). Also there are a lot of duplicated media files. Is there a way to remove duplicated products? At least i want to remove products.
I had the same problem and it was I did.
1.- In my database found duplicated products (searching by sku) with the query:
SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1
2.- I create a new table called wp_repeated with these skus:
CREATE TABLE wp_repeated AS SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1
3.- I got the post_id from these skus:
SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)
4.- And, finally, remove and delete from all tables where the post_id appears:
DELETE relations.*, taxes.*, terms.* FROM wp_term_relationships AS relations INNER JOIN wp_term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN wp_terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_postmeta WHERE post_id IN ( SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_posts WHERE ID IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated));
I hope it can be helpful to you. Regards.
TAKE CARE: these queries remove ALL repeated products.

Retrieve Wordpress post with featured image/thumbnail in one sql query

here's my code :
SELECT p.ID, p.post_title,
MAX(IF(pm.meta_key = 'featured_image', pm.meta_value, NULL)) AS event_imgID
FROM wpgp_posts AS p
LEFT JOIN wpgp_postmeta AS pm on pm.post_id = p.ID
WHERE p.post_type = 'product' and p.post_status = 'publish'
GROUP BY p.ID
event_imgID retrieve the ID from postmeta but the img url is stored in the wpgp_posts in the guid column
How to achieve this ?
You can join in the table to get more information about the image:
SELECT . . .
FROM (SELECT p.ID, p.post_title,
MAX(CASE WHEN pm.meta_key = 'featured_image' THEN pm.meta_value END) AS event_imgID
FROM wpgp_posts p LEFT JOIN
wpgp_postmeta pm
ON pm.post_id = p.ID
WHERE p.post_type = 'product' and p.post_status = 'publish'
GROUP BY p.ID
) pi LEFT JOIN
wpgp_posts wp
ON pi.event_imgID = wp.guid
I finally found the way to do it. I had to search thumbnail_id instead of guid :
SELECT
p1.ID, p1.post_title,
MAX(IF(pm.meta_key = 'listing_event_date', pm.meta_value, NULL)) AS event_date,
wm2.meta_value as event_img
FROM
wpgp_posts p1
LEFT JOIN wpgp_postmeta AS pm on (pm.post_id = p1.ID)
LEFT JOIN
wpgp_postmeta wm1
ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
wpgp_postmeta wm2
ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
WHERE
p1.post_status='publish'
AND p1.post_type='product'
GROUP BY p1.ID, wm2.meta_id
ORDER BY
event_date ASC
Thanks

Php mysql search - Search not working if with plural and singular

If I search "sales order" it's fetching "sales order" and "sales orders" in results. It's fetching the result with "s" also.
But if I search "sales orders" it's fetching "sales orders" only but I want "sales order" will also fetch.
I am using php mysql query.
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
and p.post_title LIKE '%sales order%'
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC
Try with this without "%"
p.post_title LIKE 'sales order'
You can also try like this
and (p.post_title LIKE '%sales order%' OR p.post_title LIKE '%sales orders%' )
I hope this will solve your issue.
I am not much sure about this query but you can check as
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
and p.post_title LIKE '%sales%' AND p.post_title LIKE '%order%'
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC
OR
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
and p.post_title LIKE '%sales%order%'
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC
use MySQL Full text search,check if FULLTEXT indexes are there
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
match (p.post_title) against ('sales order')
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC

Show wordpress post thumbnail with SQL query interacting with wp_post, wp_term_relationships and wp_terms

I have this query that when I execute it, it pulls 3 single posts, which I chose based on their id. The only thing that doesn't get pulled is the post thumbnail. I've tried intercepting with post_meta joining with post_id etc, but no success at all
Here's what I got until now
SELECT *
FROM wp_posts p
#LEFT JOIN wp_postmeta pm ON (pm.post_id = p.ID AND pm.meta_value IS NOT NULL AND pm.meta_key = '_thumbnail_id')
#LEFT JOIN wp_postmeta pm ON (pm.meta_value = pm.post_id AND pm.meta_key = '_wp_attached_file' AND pm.meta_value IS NOT NULL)
#LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_wp_attachment_metadata'
LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
LEFT OUTER JOIN wp_terms t ON t.term_id = r.term_taxonomy_id
WHERE p.ID in ('800','808', '569')
AND p.post_type in ('post', 'attachment')
AND p.post_status = 'publish'
The ones with hash-tags are my failed attempt, I get null value on meta_key column

sql statement for querying wp posts from specific category

i want to query wordpress posts from specific category parent but the problem is that there is no such column on wp_posts table so i need to join but my skills on the sql is not good so i need some help ,
here is the query i use for querying posts
$query = "SELECT c.*
FROM {$wpdb->prefix}posts p,
{$wpdb->prefix}comments c WHERE p.ID = c.comment_post_ID AND c.comment_approved > 0 AND p.post_type = 'product' AND p.post_status = 'publish' AND
p.comment_count > 0 ORDER BY ".$order_by." LIMIT 0, ". $number_of_comments;
}
and here is some snippet i found for joining term_taxonomy_id
$answer = $wpdb->get_results("SELECT post_title, post_content, term_taxonomy_id FROM wp_posts LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id WHERE SUBSTRING(post_title,1,1)='T' AND term_taxonomy_id=6");
the proplem i can't seem to figure how to use this example on my query neither thinking of simpler solutions so i can query from specific parent category , thanks for your help
here is the query i used and worked ..
$query = "SELECT c.*
FROM
{$wpdb->prefix}comments c ,
{$wpdb->prefix}posts p JOIN $wpdb->term_relationships TR
ON p.ID=TR.object_id
JOIN $wpdb->term_taxonomy T
ON TR.term_taxonomy_id=T.term_taxonomy_id
JOIN $wpdb->terms TS
ON T.term_id = TS.term_id
WHERE p.ID = c.comment_post_ID AND c.comment_approved > 0 AND p.post_type = 'product' AND p.post_status = 'publish' AND
p.comment_count > 0 AND T.taxonomy = 'product_cat' AND T.term_id='$term_cat' ORDER BY ".$order_by." LIMIT 0, ". $number_of_comments;
}
you would be looking at something like this:
global $wpdb;
$wpdb->show_errors();
$ur=$wpdb->get_results( $wpdb->prepare(
"
SELECT *
FROM $wpdb->posts P
JOIN $wpdb->term_relationships TR
ON P.ID=TR.object_id // identify link column
JOIN $wpdb->term_taxonomy T
ON TR.term_taxonomy_id=T.term_taxonomy_id
JOIN $wpdb->terms TS
ON T.term_id = TS.term_id
WHERE P.post_type = %d // use wild chars (define below, this one equals carmarket)
AND P.post_status = %f
AND T.taxonomy= %s
",
'carmarket',
'publish',
'carmake'
) );
$wpdb->print_error();
var_dump($ur);

Categories