I have Three tables and I join the table and retrieve all the values in this query
sql query:
SELECT wp_term_relationships.object_id, wp_posts.post_title,
wp_postmeta.meta_key, wp_postmeta.meta_value
FROM `wp_posts` , wp_postmeta, wp_term_relationships
WHERE (wp_posts.ID = wp_postmeta.post_id)
AND (wp_postmeta.post_id = wp_term_relationships.object_id)
AND wp_term_relationships.term_taxonomy_id =33
Its Show Table structure is this
object_id post_title meta_key meta_value
302 CHICKEN CHOW MEIN post_image url1
302 CHICKEN CHOW MEIN price 6.95
I want Show the OBject Id in one and merge the two rows in one column i want like this
object_id post_title meta_value(price) meta_value(post_image)
302 CHICKEN CHOW MEIN 6.95 url1
You could join to wp_postmeta twice, using more usual join syntax
SELECT rel.object_id, p.post_title,
meta_price.meta_key, meta_price.meta_value,
meta_image.meta_key, meta_image.meta_value
FROM `wp_posts` p
inner join wp_postmeta meta_price on meta_price.post_id = p.ID
inner join wp_postmeta meta_image on meta_image.post_id = p.ID
inner join wp_term_relationships rel on rel.objet_id = p.ID
WHERE rel.term_taxonomy_id = 33
and meta_price.meta_key = 'price'
and meta_image.meta_key = 'post_image'
(Syntax not checked – no access to a WordPress db here – and you'll need to change the selected columns slightly.)
If it was me, I'd simply stick with your existing query, and handle the pivot in the application code, so.
*PHP goes here*
$query = "
SELECT r.object_id
, p.post_title
, m.meta_key
, m.meta_value
FROM wp_posts p
JOIN wp_postmeta m
ON m.post_id = p.ID
JOIN wp_term_relationships r
ON r.object_id = m.post_id
WHERE r.term_taxonomy_id = 33
ORDER
BY post_title
, meta_key;
";
*More PHP goes here*
Syntax not checked, but something like this?
SELECT object_id, post_title, max(meta_key_price) as key_price, max(meta_key_image) as key_image FROM
(
SELECT
wp_term_relationships.object_id,
wp_posts.post_title,
case meta_key when 'price' then meta_value else null end as meta_key_price,
case meta_key when 'post_image' then meta_value else null end as meta_key_image
FROM `wp_posts` , wp_postmeta, wp_term_relationships
WHERE (wp_posts.ID = wp_postmeta.post_id)
AND (wp_postmeta.post_id = wp_term_relationships.object_id)
AND wp_term_relationships.term_taxonomy_id =33
) as t group by object_id
Related
i wanna update thumbnails of all products in specific categories of my shop with one image.
I know the categories id and their term_taxonomy_id but I cannot do proper update statement with subquery ...
Here is my code..
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
AND
wp_postmeta.posts_id = (
SELECT wp_posts.* FROM wp_term_relationships
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id
WHERE post_type = 'product' AND taxonomy = 'product_cat'
AND wp_term_taxonomy.term_taxonomy_id = '130')
Where I make mistake ??
product category id 114 and in term_taxonomy_id 130
This part of the question works good but changes ALL products thumbanils not only from specific category
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
Maybe I will ask for help not for resolving my problem in my way :)
This query gives some records
SELECT wp_posts.* FROM wp_term_relationships
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id
WHERE post_type = 'product' AND taxonomy = 'product_cat'
AND wp_term_taxonomy.term_taxonomy_id = '130'
and I want to use ID (from results of the query above) as wp_postmeta.post_id to change some values in those records in wp_postmeta in the query below
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
Anyone ?? Help plis....
Can any one re-write this SQL query to perform better?
At present this query takes about 29 seconds to execute in BlueHost server while it takes 6 seconds in HostGator.com server.
My intention is to delete all records exists in wp_postmeta table of post type='attachment' and with meta_key='_mycbgenie_managed_by'
DELETE FROM wp_postmeta
WHERE post_id IN
( SELECT ID FROM wp_posts
WHERE post_type = 'attachment'
AND post_parent IN
( SELECT ID FROM
( SELECT ID FROM wp_posts a
LEFT JOIN wp_postmeta b ON (a.ID = b.post_id)
LEFT JOIN wp_postmeta AS mt1 ON (a.ID = mt1.post_id)
WHERE post_type = 'product' AND mt1.meta_key = '_mycbgenie_managed_by'
) AS taskstodelete
)
)
DELETE PM FROM wp_postmeta as PM
INNER JOIN
wp_posts as P
ON PM.post_id=P.ID AND P.post_type='attachment'
INNER JOIN wp_posts as P2
ON P.post_parent=P2.ID
INNER JOIN wp_postmeta AS PM2
ON P2.ID = PM2.post_id
WHERE P2.post_type = 'product' AND PM2.meta_key = '_mycbgenie_managed_by'
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
First I would like to mention I have limited skills when it comes to MYSQL an JOIN. However this is what I have and what I like to achieve:
I have the default WordPress tables and like to get a result with post_name, title, status and the meta_value from a certain meta key.
This is what I have:
SELECT
wp_posts.ID, wp_posts.post_name, wp_posts.post_title, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN
wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
INNER JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE (
wp_term_relationships.term_taxonomy_id
IN ( 1, 2, 3 )
)
AND wp_posts.post_type = 'my_post_type'
AND (
wp_posts.post_status
IN (
'my_status_1', 'my_status_2'
)
)
AND wp_postmeta.meta_key = 'my_meta_key'
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
Everything works as expected when each post has a postmeta of 'my_meta_key'. But if the 'my_meta_key' is missing the post is not in the result.
I guess it' caused by the second INNER JOIN but as mentioned I have no idea what I should it replace it with.
I'm sure it's something simple
Move the AND wp_postmeta.meta_key = 'my_meta_key' to
INNER JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
Like this and change the INNER for a LEFT
LEFT JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'my_meta_key')
If you put your condition in the "Where Clause" The Left join will be "overwritten".
And I will add an advice. Dont use parenthesis when you dont really need it to keep your code easy to read.
The following returns
all records from WP_Posts
only those records with WP_TERM_RELATIONSHIPS
only those records in wp_postmeta which a matching record in wp_posts.
*
SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title,
wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
LEFT JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_term_relationships.term_taxonomy_id IN ( 1, 2, 3 )
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_statusIN ('my_status_1', 'my_status_2')
AND (wp_postmeta.meta_key = 'my_meta_key' or wp_postmeta.meta_key is null)
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
you need the is null otherwise records in wp_posts without wp_postmeta data will be excluded.
Change the INNER JOIN on the wp_postmeta table to a LEFT JOIN:
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
And move the WHERE filter for the wp_postmeta to the JOIN condition.
So your query will be:
SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN wp_term_relationships
ON ( wp_posts.ID = wp_term_relationships.object_id )
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
AND wp_postmeta.meta_key = 'my_meta_key'
WHERE wp_term_relationships.term_taxonomy_id IN ( 1, 2, 3 )
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_status IN ('my_status_1', 'my_status_2')
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
The INNER JOIN syntax will only return rows that match in both tables. So if you do not have a matching row, you will not get any result. By changing that to a LEFT JOIN, you will return all rows even if there is not a matching row in the wp_postmeta table. If the row does not exist, then the values from the wp_postmeta table will be null.
I need to extract all posts from my WordPress DB along with the associated categories and not sure how to write this query. I've taken a couple of stabs at it already with no joy and would appreciate the help?
EDIT: Here's what I have tried already:
SELECT post_title, wpr.object_id, wp_terms.name
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
INNER JOIN wp_posts ON ID = wpr.object_id
WHERE taxonomy = 'category'
AND post_type = 'post'
ORDER by post_title
This seems to work but it returns 1,553 where I know I only have 1343 in my DB.
EDIT:
We did the same thing on another SQL query a little while ago and found that it was pulling in the revisions and other post types but thought that this was resolved using post_type = 'post'
EDIT:
Upon looking at the number of categories in the DB, I come up with a total number of 216, 6 off the number if you subtract 1553 - 1343 = 216. So I think this total number of 1553 is coming from the wp_terms table which needs to be excluded and only those that are active with published posts should be shown?
EDIT:
The other possibility is that each post can have multiple categories, hence the reason for having more posts (1553). So how could I separate each posts into multiple categories?
Many thanks!
This is the final answer that worked for me.
SELECT DISTINCT
post_title
, post_content
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (US\$)' AND wp_postmeta.post_id = wp_posts.ID) AS "Asking Price (US\$)"
,(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= 'category' and wp_posts.ID = wpr.object_id
) AS "Categories"
,(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= 'post_tag' and wp_posts.ID = wpr.object_id
) AS "Tags"
FROM wp_posts
WHERE post_type = 'post'
ORDER BY
post_title
, post_content
/* Query for fetch post/posts using post user, post category and post_title */
$query ="SELECT wp_posts.post_title, wp_posts.post_content, wp_posts.comment_count, wp_users.display_name, wp_terms.name
FROM wp_posts
JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id)
JOIN wp_users ON (wp_posts.post_author = wp_users.ID)
WHERE wp_term_taxonomy.term_id IN ($bycat)
AND wp_users.ID = $byuser
AND wp_posts.post_type = 'post'
AND (wp_posts.post_content LIKE '$bytitle' OR wp_posts.post_title LIKE '$bytitle')
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_modified DESC";
/*---- FOR DISPLAY RESULT -----*/
$resultfirst = $wpdb->get_results($query);
foreach( $resultfirst as $result ){
echo $result->post_title .'';
echo $result->display_name.'';
echo $result->name.'';
echo $result->comment_count.'';
}