I am working on Wordpress, I want to get
ID in 'posts' table where posts.ID = postmeta.post_id AND postmeta.distance > postmeta.radius
And now the SQL statement:
SELECT DISTINCT p.ID
FROM posts p
LEFT JOIN postmeta m ON p.ID = m.post_id
WHERE p.post_type = 'babysitter'
AND p.post_status = 'publish'
AND m.meta_key = 'distance'
AND ( m.meta_value > (SELECT meta_value
FROM postmeta
WHERE meta_key = 'radius' ))
Besides the problem of avoiding nested SELECT I don't get what I want
Well, you explanation seems weird.. You say postmeta.distance > postmeta.radius but you select it from hlp_postmeta , also M alias seem not to do anything. So, I'm guessing you have a few typos here, Either you want it from postmeta like you said, and then I think this is what you need:
SELECT DISTINCT p.ID
FROM posts p
LEFT JOIN postmeta m ON p.ID = m.post_id
LEFT JOIN postmeta m2 ON p.ID = m2.post_id
WHERE p.post_type = 'babysitter'
AND p.post_status = 'publish'
AND m2.meta_key = 'distance'
AND m.meta_key = 'radius'
AND m2.meta_value > m.meta_value
Or you its not like you said and its from hlp_postmeta and then:
SELECT DISTINCT p.ID
FROM posts p
LEFT JOIN postmeta m ON p.ID = m.post_id
LEFT JOIN hlp_postmeta m2 ON p.ID = m2.post_id
WHERE p.post_type = 'babysitter'
AND p.post_status = 'publish'
AND m2.meta_key = 'distance'
AND m.meta_key = 'radius'
AND m2.meta_value > m.meta_value
Of course I can be wrong in both cases, if so tell me and I'll correct it.
Related
I found a very similar topic on here from a long while back where the poster was asking for a way of returning all products in WooCommerce that didn't have a thumbnail (ie no image).
That works great, but what I also want to return is the SKU for the product, which also exists in the wp_postmeta table.
How would I adjust the query below to return that?
Thanks in advance!
SELECT p.ID, p.post_title
FROM wp_posts as p LEFT OUTER JOIN wp_postmeta pm ON (p.ID=pm.post_id AND pm.meta_key = '_thumbnail_id')
WHERE p.post_type = 'product' AND (meta_key IS NULL OR meta_value = "")
Think I've answered my own question after messing around!
SELECT wp_posts.ID, wp_posts.post_title, wp_postmeta1.meta_value as SKU
FROM wp_posts
LEFT OUTER JOIN wp_postmeta pm ON (wp_posts.ID=pm.post_id AND pm.meta_key = '_thumbnail_id')
LEFT JOIN wp_postmeta wp_postmeta1 on wp_postmeta1.post_id = wp_posts.ID and wp_postmeta1.meta_key = '_sku'
WHERE wp_posts.post_type = 'product' AND (pm.meta_key IS NULL OR pm.meta_value = "")
With $wpdb->get_results you can use:
$result = $wpdb->get_results("SELECT ID,post_title, wp_postmeta1.meta_value as SKU FROM $wpdb->posts p
LEFT OUTER JOIN $wpdb->postmeta pm ON (p.ID=pm.post_id AND pm.meta_key = '_thumbnail_id')
LEFT JOIN $wpdb->postmeta wp_postmeta1 on wp_postmeta1.post_id = p.ID and wp_postmeta1.meta_key = '_sku'
WHERE p.post_type = 'product' AND (pm.meta_key IS NULL OR pm.meta_value = '')");
Here is the SQL request I tried:
SELECT COUNT(DISTINCT um.user_id)
FROM wp_usermeta um
LEFT JOIN wp_users u ON um.user_id = u.ID
LEFT JOIN wp_posts p ON u.ID = p.post_author AND p.post_status = 'publish' AND p.post_type = 'post'
WHERE p.post_date >= '2013-03-15'
AND u.user_login NOT LIKE 'LOCKED-%'
AND um.meta_key = 'wp_capabilities'
AND um.meta_value LIKE '%writers%'
AND COUNT(um.user_id) >= 2
The last line:
AND COUNT(um.user_id) >= 2
Is causing some errors.
Is there a way to only get the um.user_id that recur at least twice or more?
Group by the users and use the having clause to apply aggregate functions like count on each group
select count(*) from
(
SELECT um.user_id
FROM wp_usermeta um
LEFT JOIN wp_users u ON um.user_id = u.ID
LEFT JOIN wp_posts p ON u.ID = p.post_author AND p.post_status = 'publish' AND p.post_type = 'post'
WHERE p.post_date >= '2013-03-15'
AND u.user_login NOT LIKE 'LOCKED-%'
AND um.meta_key = 'wp_capabilities'
AND um.meta_value LIKE '%affiliate_pay_per_publish%'
GROUP BY um.user_id
HAVING COUNT(*) >= 2
) alias_name
I'm new to MySQL and ended up with this statement on a standard Wordpress database to get the latest articles.
However, the statement below requires that related rows are found in [wp_postmeta] to get the thumbnail belonging to the article.
However, some posts may not have a thumbnail but I still want to select those.
How can I alter the statement to not require a thumbnail value in [wp_postmeta]? I believe I need a left join clause, but don't know how to do it.
SELECT p.*, ( SELECT guid FROM wp_posts WHERE id = m.meta_value ) AS imgurl
FROM wp_posts p, wp_postmeta m
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.id = m.post_id
AND m.meta_key = '_thumbnail_id'
ORDER BY post_date DESC
Try this with join and ON(p.id = m.post_id AND m.meta_key = '_thumbnail_id' )
SELECT DISTINCT p.*, ( SELECT guid FROM wp_posts WHERE id = m.meta_value LIMIT 1) AS imgurl
FROM wp_posts p
LEFT JOIN wp_postmeta m ON(p.id = m.post_id AND m.meta_key = '_thumbnail_id' )
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
ORDER BY post_date DESC
Use this query it will fetch latest 5 post, you can increase the display limit.
$sql = " SELECT * FROM wp_posts p
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.post_type = 'post'
ORDER BY p.post_date DESC
LIMIT 5";
I'm trying to show latest Post Excerpt, Post Title and Featured Image on an ASP page. To simplify the query I add the Permalink as a custom field for each Post. I have a query that gets all except Featured Image and I have another query that gets Featured image but I can't work out how to merge them into one query.
// Gets Post Excerpt and Post Title
SELECT
wp_posts.id,
wp_posts.post_title,
wp_postmeta.meta_value,
wp_postmeta.meta_key,
wp_posts.post_excerpt
FROM
wp_postmeta
INNER JOIN wp_posts p ON (wp_postmeta.post_id = wp_posts.ID)
WHERE post_id IN (
SELECT wp_posts.id
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'
AND meta_key = 'my_permalink'
ORDER BY post_date, wp_posts.id
)
ORDER BY wp_posts.post_date DESC, wp_postmeta.post_id
LIMIT 2
// Gets Featured Images for a Post
SELECT p.*
FROM wp_postmeta AS pm
INNER JOIN wp_posts AS p ON pm.meta_value=p.ID
WHERE pm.post_id = $ID
AND pm.meta_key = '_thumbnail_id'
ORDER BY p.post_date DESC
Can anyone help me merge these queries? Thanks.
Sample data to be returned:
ID | post_title | post_excerpt | meta_value_my_permalink | featured_image_guid
** UPDATE *
I've managed to get the following which works fine except I can't get more that one row as I get an error when I try and use 'IN' in a subquery
e.g. pm2.post_id IN (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 2)
SELECT
p.post_title,
p.post_excerpt,
pm.meta_value AS permalink,
p2.guid as thumbnail,
p2.post_title as image_alt
FROM
wp_postmeta pm
INNER JOIN wp_posts p ON (pm.post_id = p.ID),
wp_postmeta pm2
INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
pm.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND
pm.meta_key = 'my_permalink' AND
pm2.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND
pm2.meta_key = '_thumbnail_id'
It is difficult to see what you are trying to get without some sample data but I think that you know the post ID and need the info from both the my_permakink and _thumbnail_id keys?
Since I'm on a tablet I will give you the bare bones only and let you limit the result set and filter it.
Select *
From wp_posts p
Inner join
Wp_postmeta pm1 on p.id = pm1.post_id and metakey = 'my_permalink'
Inner join
Wp_postmeta pm2 on p.id = pm1.post_id and metakey = '_thumbnail_id'
This will give you every post that has an image with the info for both metakeys. If you want the info for posts without an image change the second inner join to a left join.
A bunch of joins and a view as the subquery has given me what I need.
SELECT
p.post_title,
p.post_excerpt,
pm.meta_value AS permalink,
p2.guid AS thumbnail,
p2.post_title AS thumbnail_alt
FROM
wp_postmeta pm
INNER JOIN wp_posts p ON (pm.post_id = p.ID)
INNER JOIN wp_postmeta pm2 ON (pm2.post_id = p.ID)
INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
pm.meta_key = 'my_permalink' AND
pm2.meta_key = '_thumbnail_id' AND
p.ID IN (SELECT * FROM vwLatestPostIds)
Use this to retrieve the post title, post id, post featured thumbnail url, post category etc -
"SELECT a.ID id, a.post_title title, a.post_content content,taxonomy, name, a.post_date_gmt postdate,max(c.guid) img_url
FROM kb_posts a
JOIN kb_term_relationships tr
ON (a.id = tr.object_id)
JOIN kb_term_taxonomy tt
ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy='category')
JOIN kb_terms t
ON (t.term_id = tt.term_id AND t.term_id = $user_id)
LEFT JOIN
(select post_parent, max(post_date_gmt) as latest_image_date from kb_posts
where post_type='attachment' GROUP BY post_parent) b
on a.id=b.post_parent
LEFT JOIN kb_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.ID ORDER BY a.ID DESC LIMIT 5"
I need some help with a query that should return posts based on their wp-postratings score (http://wordpress.org/extend/plugins/wp-postratings/).
The user chooses a minimum rating (0 to 5 stars) and a maximum rating (0 to 5 stars) and the query should return the posts that match. I have it working where the user input for both values is above 0 but I can't seem to get my head around the 0 value. Since 0 represents unrated posts - and hence onces that have no ratings meta data - I need to select not only the posts where the rating is no more than the specified max value, but also every post that has no rating meta data.
How can I do this?? Any help will be very much appreciated!
Here's my current query:
SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score
FROM wp_posts p
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID
LEFT JOIN wp_postmeta AS t2 ON t1.post_id = t2.post_id
LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID
WHERE t1.meta_key = 'ratings_average'
AND t2.meta_key = 'ratings_users'
AND t3.meta_key = 'ratings_score'
AND p.post_date < NOW()
AND p.post_status = 'publish'
AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool')
AND ( (t1.meta_value+0.00) IS NULL OR (t1.meta_value+0.00) <= $max_stars )
ORDER BY p.post_date DESC
LIMIT 20
I had to do something similar a while back where I was running a cron job to send posts to another application that weren't already registered. The best method I found was to write a query that checked that the ID was NOT IN a query of posts with the meta key.
SELECT $wpdb->posts.ID
FROM $wpdb->posts
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.ID NOT IN (
SELECT $wpdb->posts.ID
FROM $wpdb->posts
left join $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = 'meta_key')
I believe this should work, though I obviously haven't tested it.
SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score
FROM wp_posts p
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID
LEFT JOIN wp_postmeta AS t2 ON t1.post_id = t2.post_id
LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID
WHERE t1.meta_key = 'ratings_average'
AND t2.meta_key = 'ratings_users'
AND t3.meta_key = 'ratings_score'
AND p.post_date < NOW()
AND p.post_status = 'publish'
AND (tt.taxonomy = 'post_tag'
AND tt.term_id = t.term_id
AND t.slug = 'liverpool')
AND (
p.ID NOT IN (
SELECT p.ID
FROM wp_posts AS p
LEFT JOIN wp_postmeta AS pm ON (pm.post_id = p.ID)
WHERE pm.meta_key = 'ratings_score'
)
OR
(t1.meta_value+0.00) <= $max_stars )
ORDER BY p.post_date DESC
LIMIT 20
Okay, this query seems to work for me. Its a bit ugly though and not too quick so if anyone has a better one feel free to improve upon it!
It selects all of the rated posts that are below the $max_stars value, then combines the table with a separate select which gets all of the non-rated posts:
(SELECT DISTINCT p.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score
FROM wp_posts p
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON t1.post_id = p.ID
LEFT JOIN wp_postmeta AS t2 ON t2.post_id = p.ID
LEFT JOIN wp_postmeta AS t3 ON t3.post_id = p.ID
WHERE t1.meta_key = 'ratings_average'
AND t2.meta_key = 'ratings_users'
AND t3.meta_key = 'ratings_score'
AND p.post_date < NOW()
AND p.post_status = 'publish'
AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool')
AND (t1.meta_value+0.00) <= $max_stars )
UNION
(SELECT DISTINCT p.*, NULL AS ratings_average, NULL AS ratings_users, NULL AS ratings_score
FROM wp_posts p
INNER JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN wp_terms t ON t.term_id = tt.term_id
LEFT JOIN wp_postmeta AS t1 ON (t1.post_id = p.ID AND t1.meta_key = 'ratings_score')
WHERE t1.post_id is null
AND p.post_date < NOW()
AND p.post_status = 'publish'
AND (tt.taxonomy = 'post_tag' AND tt.term_id = t.term_id AND t.slug = 'liverpool') )
ORDER BY post_date DESC