I have the following MySQL Query (querying wordpress db):
SELECT wp_posts.ID, wp_posts.post_date, wp_posts.post_content, wp_posts.post_title, wp_terms.name, wp_term_taxonomy.taxonomy
FROM wp_posts
LEFT JOIN wp_term_relationships 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_taxonomy.term_id)
WHERE 1=1
AND wp_terms.name != 'MyTagName'
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish')
ORDER BY wp_posts.post_date DESC
LIMIT 100
I am basically trying to STOP all 'wp_posts' coming back that have a tag (db table wp_terms) 'MyTagName'. But all the above seems to do is strip that tag name out from the rows returned, leaving the wp_post entry in there with the other tag entries it is tagged with.
Can anyone help me with this?
SELECT wp_posts.ID, wp_posts.post_date, wp_posts.post_content, wp_posts.post_title, wp_terms.name, wp_term_taxonomy.taxonomy
FROM wp_posts
JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.ID
JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE NOT EXISTS (
SELECT 1
FROM wp_term_relationships
JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_relationships.object_id = wp_posts.ID
AND wp_terms.name = 'MyTagName'
)
AND wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
LIMIT 100
The problem is that your current solution isn't checking all wp_terms that relate to the post, just the specific one. If there were 5 terms, one of which was 'MyTagName', then that single one would be discluded but the other 4 will still be joined.
Now regarding the:
AND wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
You don't need to put them in the big NOT EXISTS check because that post won't be picked up anyway if either of them are false.
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....
I've been trying to figure out how to get the wp_meta_value depending on the term ID.
This is where I'm at so far but my head is spinning now! If you can help, I'd much appreciate it :)
Thanks
SELECT wp_posts.post_title
FROM wp_posts
LEFT JOIN wp_term_relationships ON (
wp_posts.ID = wp_term_relationships.object_id
)
LEFT JOIN wp_term_taxonomy ON (
wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
)
WHERE wp_posts.post_status = 'publish'
AND wp_term_taxonomy.term_id =2
ORDER BY post_title DESC
LIMIT 0 , 30
This seems to work...
SELECT wp_posts.post_title, wp_postmeta.meta_value AS 'Contact Details'
FROM wp_posts
LEFT JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
LEFT JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
LEFT JOIN wp_postmeta ON ( wp_postmeta.post_id = wp_posts.ID )
WHERE wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key = 'contact'
AND wp_term_taxonomy.term_id =2
ORDER BY post_title;
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.'';
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
COUNT() and DISTINCT can i use together?
I have this sql statement:
SELECT * FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE ( wp_term_relationships.term_taxonomy_id IN (4,3) )
AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'published')
and I get two rows with the same ID => expected
adding GROUP BY wp_posts.ID will reduce the count of rows to one
Now I would like to get the number of rows with and SQL query
SELECT COUNT(*) AS cnt FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE ( wp_term_relationships.term_taxonomy_id IN (4,3) )
AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'published')
GROUP BY wp_posts.ID
I'll get as result "2" instead of "1", even with the "GROUP BY".
What is the correct statement to get the numbers of rows from the first statement?
Try this:
SELECT count(DISTINCT wp_posts.ID) as cnt FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE ( wp_term_relationships.term_taxonomy_id IN (4,3) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'published')
This will give you the count of unique IDs in result rows.
Since the first query displays 2 rows, the second query should return 2, and it does. This is working as it should.
Having that GROUP BY there makes no real logical sense.
A simple fix would be to use your original sql as a subselect and do the count based on that:-
SELECT COUNT(*) AS cnt
FROM (SELECT wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE ( wp_term_relationships.term_taxonomy_id IN (4,3) )
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'published')
GROUP BY wp_posts.ID) Sub1
Or the folowing might be OK, depending on the relationships between the tables (and so how unique wp_posts.ID is).
SELECT COUNT(DISTINCT wp_posts.ID)
FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE ( wp_term_relationships.term_taxonomy_id IN (4,3) )
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'published')
I'm trying to figure out the optimal combination of indexes for the query below. Right now its Using temporary; Using filesort and killing my vps. Queries take from 6-10 seconds.
SELECT SQL_CALC_FOUND_ROWS
wp_posts.ID
FROM
wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
INNER JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)
WHERE
1=1
AND wp_term_taxonomy.taxonomy = 'post_tag'
AND wp_terms.slug IN ('pie')
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish')
GROUP BY
wp_posts.ID
ORDER BY
wp_posts.post_date DESC
LIMIT 0, 20
Any suggestions?
How does this compare, performance-wise?
SELECT
p.ID
FROM
wp_posts AS p
WHERE
1=1
AND p.post_type = 'post'
AND p.post_status = 'publish'
AND EXISTS (
SELECT 1
FROM wp_term_relationships AS r
INNER JOIN wp_term_taxonomy AS t ON
r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'post_tag'
INNER JOIN wp_terms AS m ON
m.term_id = t.term_id AND m.slug IN ('pie')
WHERE r.object_id = wp_posts.ID
)
ORDER BY
p.post_date DESC
LIMIT 0, 20
Indexes should be on all the primary and foreign keys, on wp_terms.slug, on wp_term_taxonomy.taxonomy and a composite one over wp_posts.post_status, post_type, post_date.