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
Related
I have a problem when doing a SQL query in WordPress, I would like to have each Post ID in a single row with the meta_key columns, but I get several uneven records.
SELECT P.ID,
IF(PM.meta_key = 'nombrem', PM.meta_value, NULL) AS nombrem,
IF(PM.meta_key = 'generom', PM.meta_value, NULL) AS generom,
IF(PM.meta_key = 'tiempom', PM.meta_value, NULL) AS tiempom,
IF(PM.meta_key = 'urlm', PM.meta_value, NULL) AS urlm,
IF(PM.meta_key = 'imagenm', PM.meta_value, NULL) AS imagenm
FROM K1nG_posts AS P
LEFT JOIN K1nG_postmeta AS PM ON ( P.ID = PM.post_id )
WHERE P.post_type = 'post'
AND (P.post_status = 'publish' OR P.post_status = 'private') ORDER BY P.post_date DESC
By placing the above code I get the following in phpmyadmin, I want a single post ID to have each meta_value value in a non-diagonal horizontal column as shown.
You seeem to be looking for conditional aggregation. For this, you would need to add a GROUP BY clause and surround your conditional expressions with an aggregate function:
SELECT P.ID,
MAX(CASE WHEN PM.meta_key = 'nombrem' THEN PM.meta_value END) AS nombrem,
MAX(CASE WHEN PM.meta_key = 'generom' THEN PM.meta_value END) AS generom,
MAX(CASE WHEN PM.meta_key = 'tiempom' THEN PM.meta_value END) AS tiempom,
MAX(CASE WHEN PM.meta_key = 'urlm' THEN PM.meta_value END) AS urlm,
MAX(CASE WHEN PM.meta_key = 'imagenm' THEN PM.meta_value END) AS imagenm
FROM
K1nG_posts AS P
LEFT JOIN K1nG_postmeta AS PM ON P.ID = PM.post_id
WHERE
P.post_type = 'post'
AND (P.post_status = 'publish' OR P.post_status = 'private')
GROUP BY P.ID, P.post_date
ORDER BY P.post_date DESC
Group the data by P.id and max the ifs out
like
SELECT P.ID,
MAX(IF(PM.meta_key = 'nombrem', PM.meta_value, NULL)) AS nombrem,
MAX(IF(PM.meta_key = 'generom', PM.meta_value, NULL)) AS generom,
MAX(IF(PM.meta_key = 'tiempom', PM.meta_value, NULL)) AS tiempom,
MAX(IF(PM.meta_key = 'urlm', PM.meta_value, NULL)) AS urlm,
MAX(IF(PM.meta_key = 'imagenm', PM.meta_value, NULL)) AS imagenm
FROM K1nG_posts AS P
LEFT JOIN K1nG_postmeta AS PM ON ( P.ID = PM.post_id )
WHERE P.post_type = 'post'
AND (P.post_status = 'publish' OR P.post_status = 'private') ORDER BY P.post_date DESC
GROUP BY P.ID;
I have a wordpress website whereby I am doing an ajax request to return a json object of property details (to use with google maps). I currently have the following query:
SELECT
p.ID AS 'id',
p.post_title AS 'title',
t.name AS 'property_type',
c.name AS 'listing_type',
pm.meta_value AS 'address',
pm2.meta_value AS 'latitude',
pm3.meta_value AS 'longitude',
pm4.meta_value AS 'price',
pm5.meta_value AS 'bedrooms',
pm6.meta_value AS 'baths',
pm7.meta_value AS 'show_date',
p.guid,
wm2.meta_value AS 'image'
FROM
wp_posts p
INNER JOIN
wp_postmeta AS pm ON pm.post_id = p.ID
INNER JOIN
wp_postmeta AS pm2 ON pm2.post_id = p.ID
INNER JOIN
wp_postmeta AS pm3 ON pm3.post_id = p.ID
INNER JOIN
wp_postmeta AS pm4 ON pm4.post_id = p.ID
INNER JOIN
wp_postmeta AS pm5 ON pm5.post_id = p.ID
INNER JOIN
wp_postmeta AS pm6 ON pm6.post_id = p.ID
INNER JOIN
wp_postmeta AS pm7 ON pm7.post_id = p.ID
LEFT JOIN
wp_term_relationships AS r ON (p.ID = r.object_id)
INNER JOIN
wp_term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
INNER JOIN
wp_terms AS t ON (r.term_taxonomy_id = t.term_id)
LEFT JOIN
wp_term_relationships AS v ON (p.ID = v.object_id)
INNER JOIN
wp_term_taxonomy AS z ON (v.term_taxonomy_id = z.term_taxonomy_id)
INNER JOIN
wp_terms AS c ON (v.term_taxonomy_id = c.term_id)
LEFT JOIN
wp_postmeta wm1 ON (wm1.post_id = p.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id')
LEFT JOIN
wp_postmeta wm2 ON (wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL)
WHERE
pm.meta_key = 'property_address'
AND pm2.meta_key = 'property_lat'
AND pm3.meta_key = 'property_lng'
AND pm4.meta_key = 'property_price'
AND pm5.meta_key = 'property_beds'
AND pm6.meta_key = 'property_baths'
AND pm7.meta_key = 'property_show_date'
AND x.taxonomy = 'property-type'
AND z.taxonomy = 'listing-type'
AND p.post_type = 'property'
AND p.post_status = 'publish'
which returns the data perfectly:
id | title | property_type | listing_type | address | latitude | longitude | price | bedrooms | baths | show_date | guid
however, as soon as I have more than one property on the website the query seems to struggle and even breaks the database (Completely lost the ability to connect to db taking the website down, having to start again with a new db). I have pinned the problem down to the taxonomy part of my query:
LEFT JOIN
wp_term_relationships AS r ON (p.ID = r.object_id)
INNER JOIN
wp_term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
INNER JOIN
wp_terms AS t ON (r.term_taxonomy_id = t.term_id)
LEFT JOIN
wp_term_relationships AS v ON (p.ID = v.object_id)
INNER JOIN
wp_term_taxonomy AS z ON (v.term_taxonomy_id = z.term_taxonomy_id)
INNER JOIN
wp_terms AS c ON (v.term_taxonomy_id = c.term_id)
but I have no idea how to improve on this.
Does anyone have any ideas?
$query = "SELECT DISTINCT
id,
post_title,
guid,
pm.meta_value AS 'address',
pm2.meta_value AS 'latitude',
pm3.meta_value AS 'longitude',
pm4.meta_value AS 'price',
pm5.meta_value AS 'bedrooms',
pm6.meta_value AS 'baths',
pm7.meta_value AS 'show_date',
wm2.meta_value AS 'image',
(SELECT
GROUP_CONCAT(wp_terms.name
SEPARATOR ', ')
FROM
wp_terms
INNER JOIN
wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN
wp_term_relationships wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE
taxonomy = 'property-type'
AND wp_posts.ID = wpr.object_id) AS 'property_type',
(SELECT
GROUP_CONCAT(wp_terms.name
SEPARATOR ', ')
FROM
wp_terms
INNER JOIN
wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN
wp_term_relationships wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE
taxonomy = 'listing-type'
AND wp_posts.ID = wpr.object_id) AS 'listing_type'
FROM
wp_posts
INNER JOIN
wp_postmeta AS pm ON pm.post_id = wp_posts.ID
INNER JOIN
wp_postmeta AS pm2 ON pm2.post_id = wp_posts.ID
INNER JOIN
wp_postmeta AS pm3 ON pm3.post_id = wp_posts.ID
INNER JOIN
wp_postmeta AS pm4 ON pm4.post_id = wp_posts.ID
INNER JOIN
wp_postmeta AS pm5 ON pm5.post_id = wp_posts.ID
INNER JOIN
wp_postmeta AS pm6 ON pm6.post_id = wp_posts.ID
INNER JOIN
wp_postmeta AS pm7 ON pm7.post_id = wp_posts.ID
INNER JOIN
wp_postmeta wm1 ON (wm1.post_id = wp_posts.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id')
INNER JOIN
wp_postmeta wm2 ON (wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL)
WHERE
post_type = 'property'
AND pm.meta_key = 'property_address'
AND pm2.meta_key = 'property_lat'
AND pm3.meta_key = 'property_lng'
AND pm4.meta_key = 'property_price'
AND pm5.meta_key = 'property_beds'
AND pm6.meta_key = 'property_baths'
AND pm7.meta_key = 'property_show_date'";
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
I would like to retrieve a thumbnail version of the "featured image"
this is the code I have but it is pulling the large image.
SELECT a.post_title title, max(c.guid) img_url, a.ID id, a.post_name
FROM wp_posts a
LEFT JOIN
(select post_parent, max(post_date_gmt) as latest_image_date
from wp_posts
where post_type='attachment'
GROUP BY post_parent) b
on a.id=b.post_parent
LEFT JOIN
wp_posts c
on c.post_parent=a.id
and c.post_type='attachment'
and b.latest_image_date = c.post_date_gmt
WHERE c.guid IS NOT NULL
GROUP BY a.post_title
ORDER BY a.ID
May be this query works for U. It's working absolutely fine for me.
SELECT p1.*, wm2.meta_value
FROM wp_posts p1
LEFT JOIN
wp_postmeta wm1 ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
wp_postmeta wm2 ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
LEFT JOIN
wp_term_relationships wtr ON (
object_id=p1.id
)
WHERE
p1.post_status='publish'
AND p1.post_type='post'
AND `term_taxonomy_id`=?
ORDER BY p1.post_date DESC
LIMIT 0,10
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