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
Related
Using Get orders total purchases amount for the day in Woocommerce answer code, returns the total value of woocommerce orders which works fine, but the problem is if I want to exclude all the orders where _billing_first_name key has not abc value, like in this code attempt:
global $wpdb;
return $wpdb->get_var( "
SELECT DISTINCT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-processing','wc-completed')
AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400))
AND pm.meta_key LIKE '_order_total'
AND NOT (pm.meta_value = 'abc')
" );
I have tried a number of ways with no luck, any help is appreciated
You can also use 2 INNER JOIN for the same table with a different reference as following (to avoid a double query like in your answer):
global $wpdb;
return $wpdb->get_var( "
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
WHERE p.post_type = 'shop_order'
AND p.post_status IN ('wc-processing','wc-completed')
AND pm.meta_key = '_order_total'
AND pm2.meta_key = '_billing_first_name'
AND pm2.meta_value != 'abc'
" );
Tested and works smoother.
For anyone looking into this the fix i made is:
global $wpdb;
return $wpdb->get_var( "
SELECT DISTINCT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-processing','wc-completed')
AND pm.meta_key LIKE '_order_total'
AND pm.post_id not in(SELECT post_id from wp_postmeta WHERE meta_key = '_billing_first_name' AND meta_value ='abc' )
" );
I working on my search function and I have an Issue
My query:
$query = " SELECT p.ID, p.post_title
FROM $posts p
LEFT JOIN $postmeta pm ON (pm.post_id = p.ID AND pm.meta_key = '_sku')
WHERE p.post_type = 'product'
AND (
p.ID LIKE '%$keywords%' OR
p.post_title LIKE '%$keywords%' OR
IF(pm.meta_value IS NULL, 0, pm.meta_value LIKE '%$keywords%')
)
ORDER BY p.post_title";
In the column post_title I have a value like Remy Martin XO
Now you can search on Remy or Martin or Remy Matin and there are results.
But if you search on Martin Remy there are no results.
What I tried so far:
I changed the query and replaced the LIKE to CONTAINS but now I have no results on any search
$query = " SELECT p.ID, p.post_title
FROM $posts p
LEFT JOIN $postmeta pm ON (pm.post_id = p.ID AND pm.meta_key = '_sku')
WHERE p.post_type = 'product'
AND (
p.ID LIKE '%$keywords%' OR
--> CONTAINS(p.post_title, '$keywords') <-- OR
IF(pm.meta_value IS NULL, 0, pm.meta_value LIKE '%$keywords%')
)
ORDER BY p.post_title";
Who can help me with this?
Please replace your line with this line. Hopefully it will work.
CONTAINS(p.post_title, '$keywords',1) > 0
If it is my SQL then use INSTR(str,substr). You can search this Syntex you will get lots of example.
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.
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
I have a query that connects to a wordpress db and returns some posts.
In order to get the image for each of these posts i run another query inside a php foreach loop
The problem is that running the second query inside the foreach loop is extremely slow and i need another way to merge these 2 queries into 1.
The first query
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value
IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%')
Based on the returned post ids, i need for each of these ids
the featured image.
This query does this job, but only for 1 id only.
The second query
SELECT wp_posts.guid
FROM wp_posts
WHERE wp_posts.ID =
(Select wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key =
'_thumbnail_id' AND wp_postmeta.post_id = 'The id of each post')
I need a query to return all the posts alongside with its images.
I really don't understand your data structure, but try this query
select
t1.*, t2.guid
from (
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%'
)
) t1
left join (
select
p2.guid, pm2.post_id
from
wp_posts p2
join wp_postmeta pm2 on
pm2.meta_value = p2.ID
and pm2.meta_key = '_thumbnail_id'
) t2 on t2.post_id = t1.ID