I want to retrieve all the records that have a
meta_key = 'veteran_state'
where I pass in the state to return all the records of veterans from that state.
The wp_metapost has 3 meta_keys:
veteran_state
veteran_name
veteran_website
I have been working on the following query in MySQL:
SELECT *
FROM wp_postmeta
LEFT JOIN wp_posts ON wp_posts.id = wp_postmeta.post_id
WHERE wp_posts.post_type = 'veterans'
AND wp_postmeta.meta_value = 'colorado'
AND wp_posts.post_status = 'publish'
However, it returns only the veteran_state value.
I would like to return all the values for that state and render them to the browser as:
Veteran Website
John Doe http://website.com
Mary Veteran http://mywebsite.com
Thank you in advance for any help.
Try this: do the filtering on the individual table and then join the results.
SELECT A.* FROM
(SELECT * FROM wp_postmeta WHERE meta_value='colorado') A LEFT JOIN
(SELECT * FROM wp_posts WHERE post_type='veterans' AND post_status='publish') B
ON A.post_id=B.id;
Related
I need to use some library to export product names, SKU and prices to one CSV file. This library connects using PDO and needs an SQL query.
I want to select 'name', 'SKU' and 'price' from 2 WordPress tables, namely wp_posts and wp_postmeta.
I don't know how to get data from 'meta_value' column twice for 'meta_key'='_price' and 'meta_key'='_sku', ex.
My current query:
"SELECT a.post_title, m1.meta_value, m2.meta_value FROM wp_posts a, wp_postmeta m1, wp_postmeta m2
WHERE a.post_type='product' AND m1.post_id = a.ID
AND m1.meta_key='_sku'
AND m2.meta_key='_price'"
It sounds like you could do with a join so you're relating the meta information to the right posts.
SELECT
post.post_title,
meta.meta_value
FROM wp_posts AS post
LEFT JOIN wp_postmeta AS meta
ON post.post_id = meta.post_id
WHERE post.post_type = 'product'
AND meta.meta_key IN ('_sku', '_price')
Example results:
post_title | meta_value
--------------|-----------
Cheddar | CHE001
Cheddar | 2.45
Red Leicester | CHE002
...
This assumes that the id column in wp_posts is post_id.
It's important to note that this will return up to two rows for each post, depending on whether it has a meta row for _sku and _price). If you need the data all on the same row (as you might for your export) you might need something like this instead:
SELECT
post.post_title,
metaSku.meta_value AS sku,
metaPrice.meta_value AS price
FROM wp_posts AS post
LEFT JOIN (
SELECT
*
FROM wp_postmeta
WHERE meta_key = '_sku'
) AS metaSku
ON post.post_id = metaSku.post_id
LEFT JOIN (
SELECT
*
FROM wp_postmeta
WHERE meta_key = '_price'
) AS metaPrice
ON post.post_id = metaPrice.post_id
WHERE post.post_type = 'product'
Example results:
post_title | sku | price
--------------|--------|------
Cheddar | CHE001 | 2.45
Red Leicester | CHE002 |
...
I hope this helps.
This will works for me
SELECT post.post_title, metaSku.meta_value AS sku, metaPrice.meta_value AS price FROM wp_posts AS post LEFT JOIN ( SELECT * FROM wp_postmeta WHERE meta_key = '_sku' ) AS metaSku ON post.ID = metaSku.post_id LEFT JOIN ( SELECT * FROM wp_postmeta WHERE meta_key = '_price' ) AS metaPrice ON post.ID = metaPrice.post_id WHERE post.post_type = 'product';
I have complex SELECT request:
select meta_value
from wp_posts v
left join wp_postmeta pm on (pm.post_id = v.id)
left join wp_posts p on (v.post_parent = p.id)
where meta_key in ('_price','_regular_price')
and v.post_type = 'product_variation'
and p.id = '1743'
limit 0,100
It returns me 4 (four) needed fields with values like
400
500
300
350
I need to update these values and set their values equal, for example 1000.
Can I, based on my SELECT, run an UPDATE query?
Any condition used in a Select query could be used in Update query, of course it might need a little modification. What you can do in your case is the following:
meta_value
Update wp_posts
set meta_value = 1000
where id IN (select v.id
from wp_posts v
left join wp_postmeta pm on (pm.post_id = v.id)
left join wp_posts p on (v.post_parent = p.id)
where v.meta_key in ('_price','_regular_price')
and v.post_type = 'product_variation'
and p.id = '1743')
Please note that I didn't test this, so I am not totally sure it will work. Make sure to backup your database before any queries like this. Hopefully it will give you the desired results.
Edit: I have assumed that id in wp_posts is a unique value.
I've have two sql queries which I'm trying to combine
The first:
SELECT * FROM wp_posts
JOIN wp_postmeta on (post_id=ID)
WHERE meta_key = "packageID" and meta_value = 1
ORDER BY post_date limit 50
Joins the wordpress wp_post table to the wp_postmeta and gets all the posts meeting with packageID = 1 (I think it might be an inelegant way of doing it but it works)
The second
SELECT * FROM wp_postmeta
JOIN wp_posts ON (meta_value=ID)
WHERE post_id = 2110
AND meta_key = '_thumbnail_id'
again joins the wp_post table to the wp_postmeta table, so for the post with the id 2110 it successfully gets the thumbnail for that posts. NB 2110 is just an example of an id
In Wordpress a thumbnail is a kind of post. So in this example the text which constitutes post 2110 is a associated with post 2115 - the latter being the thumbnail
What I'm trying to do is get the list as in the first query but also get thumbnails associated with each post
I think I need two joins but I can't see how to do it (being an sql beginner)
NB this will be in a script outside Wordpress so I can't use Wordpress's built-in functions
You can try this one,if there are more than one thumbnails for the post you can get the list of thumbnails separated by comma
SELECT
*,
(SELECT
GROUP_CONCAT(meta_value)
FROM
wp_postmeta
WHERE post_id = wp.ID
AND wpm.meta_key = "_thumbnail_id") AS `thumbnails`
FROM
wp_posts wp
JOIN wp_postmeta wpm
ON (wpm.post_id = wp.ID)
WHERE wpm.meta_key = "packageID"
AND wpm.meta_value = 1
ORDER BY wp.post_date
LIMIT 50
Note : GROUP_CONCAT has a limit to concat characters but you
can increase this limit
To get only one thumbnail you can try this
SELECT
*,
(SELECT
(meta_value)
FROM
wp_postmeta
WHERE post_id = wp.ID
AND wpm.meta_key = "_thumbnail_id" LIMIT 1)
FROM
wp_posts wp
JOIN wp_postmeta wpm
ON (wpm.post_id = wp.ID)
WHERE wpm.meta_key = "packageID"
AND wpm.meta_value = 1
ORDER BY wp.post_date
LIMIT 50
try with the following code
SELECT * FROM wp_posts wp JOIN wp_postmeta wm on (wp.post_id=wm.ID) WHERE wp.meta_key = "packageID" and wp.meta_value = 1 ORDER BY wp.post_date limit 50;
use proper alias and try it.
Try using the post_type column. The attachments have a post_type of 'attachment'. I can further explain if needed.
Also the post to which the thumbnail is attached to will be in the column post_parent.
global $wpdb;
$query7 = "SELECT distinct wp_postmeta.meta_value, wp_postmeta.meta_key, wp_posts.ID
FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key = 'packageID'
AND wp_postmeta.meta_value = 1
AND (mt1.meta_key LIKE '_thumbnail_id')
$output = $wpdb->get_results( $query7 );
Use join with different aliases when joining same table more than once.
Hope this helps.
Try this
SELECT * FROM wp_posts P1
LEFT JOIN wp_postmeta M1 ON (M1.post_id=P1.ID)
WHERE (M1.meta_key = "packageID" and M1.meta_value = 1 )
LEFT JOIN wp_postmeta M2 ON (M2.meta_key=P1.ID AND M2.meta_key = '_thumbnail_id')
LEFT JOIN wp_posts P2 ON (M2.meta_value=P2.ID)
ORDER BY P1.post_date limit 50
I'm using WordPress. I want to get everything from the wp_posts table and two values from the postmeta table.
The "wp_postmeta" has the columns "post_id", "meta_key", and "meta_value".
So far, I'm able to get one of the "meta_value":
SELECT wp_post.*, $wpdb->postmeta.meta_value AS votes
FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE post_type='post'
AND wp_postmeta.meta_key = 'votes'
However, I also want to get another "meta_value" with the same "post_id" but a different "meta_key". How can I extend this query to the "meta_value"?
If you don't want multiple rows per post, you can do this by having multiple joins:
SELECT wp_post.*, metavotes.meta_value AS votes, metaother.meta_value AS other
FROM wp_posts
INNER JOIN wp_postmeta metavotes
ON ( wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'votes')
INNER JOIN wp_postmeta metaother
ON ( wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'other')
WHERE post_type='post'
(this assumes there is always exactly 1 row for each piece of metadata in wp_postmeta. I'm not familiar enough with wordpress to know whether this is the case. If metadata is optional, use LEFT OUTER JOIN's instead. If you can have multiple, what kind of output do you want?)
How about adding your additional meta_key to the query with the IN clause. Let's say it's the string value foo that you want to add:
SELECT wp_post.*, $wpdb->postmeta.meta_value AS votes
FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE post_type='post'
AND wp_postmeta.meta_key IN ('votes', 'foo');
Maybe try something like this:
SELECT wp_posts.*, wp_postmeta.meta_value, wp_postmeta.meta_key
FROM wp_posts INNER JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id)
WHERE wp_posts.post_type='post'
AND wp_postmeta.meta_key IN ('votes', ...)
What i want is:
Get posts with date greater then 2010-03-02 and with the meta_value 'something' + like '2010-'
because there are other values like 239048192304 or 293811743
$query = "SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.ID
WHERE wp_postmeta.meta_value >='2010-03-02'
AND wp_postmeta.meta_value = 'something'
AND wp_postmeta.meta_value LIKE '2010-'
ORDER BY wp_postmeta.meta_value ASC
LIMIT 0,10";
can you help me out please? thank you!
Update2:
table wp_postmeta
post_id | meta_value
5 | 2010-12-30
5 | Berlin
3 | 2010-12-29
3 | Paris
2 | 2009-12-29
2 | Paris
14 | 12232456521
14 | Berlin
Output:
2010-12-30 Berlin ID 5
2010-12-29 Paris ID 3
Maybe you mean an OR instead of an AND?
...
WHERE wp_postmeta.meta_value >= '2010-03-02' OR
wp_postmeta.meta_value = 'something' OR
wp_postmeta.meta_value LIKE '2010-'
Unfortunately in the English language, AND and OR can be used interchangeably in certain cases:
"I always carry an umbrella for when it rains and snows."
"I always carry an umbrella for when it rains or snows."
The above wouldn't be equivalent for computers :)
A larger data set and sample answer would help clarify the question but here is my interpretation of what you are looking for. It's not elegant but if you've got the buffer space allocated it works.
SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.ID
WHERE wp_postmeta.post_id IN (
select post_id from wp_postmeta where str_to_date(meta_value, '%Y-%m-%d') >= 2010-03-02' and post_id in (select post_id from wp_postmeta where meta_value = 'something')
);
For getting post information and their post meta values you need the following query:
SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID,
wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_date >= '2010-03-02'
AND EXISTS (SELECT 1 from wp_postmeta m1
WHERE m1.post_ised = wp_posts.ID
AND wp_postmeta.meta_value = 'something'
AND EXISTS (SELECT 1 from wp_postmeta m2
WHERE m2.post_ised = wp_posts.ID
AND wp_postmeta.meta_value LIKE '2010-%')
ORDER BY wp_postmeta.meta_value
ASC LIMIT 0, 10
Try this:
$query = "SELECT p.ID, m1.meta_value, m2.meta_value, p.post_title FROM
wp_posts p, wp_postmeta m1, wp_postmeta m2
WHERE p.post_date > '2010-03-02' AND
m1.post_id=p.ID AND m2.post_id=p.ID AND
m2.meta_value LIKE '2010-%' AND
m1.meta_value = 'something'
ORDER BY m1.meta_value, m2.meta_value
LIMIT 0,10";
No need for the distinct, since we're showing everything on one row anyway.
Those statements contradict themselves. First you are asking for it to be >= a date. So is meta_value a date? Then you say it must be equaled to 'something', so now it is a string and not a date. Finally you say have it be like 2010-, so now we are back to a string or a date, but no wild card % in the like so you are basically saying it has to be equaled to 2010- as well.
What is the value stored in meta_value?
Are you sure you do not mean to query multiple fields?
UPDATE
Based on the new information, I think this is what you want:
$query = "SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.ID
WHERE (wp_postmeta.meta_value = 'something'
OR wp_postmeta.meta_value LIKE '2010-%')
ORDER BY wp_postmeta.meta_value ASC
LIMIT 0,10";
Hopefully that is what you were after. I removed the 2010 >= condition given that the 2010 LIKE condition will pull that same data.