WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a RAW SQL, as I am working from different platform with WordPress Database.

When I run example.com/tag/first+second, WordPress runs a query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (10649,7783,6336,4556,3938)

Where did this get the post IDs? I am only guessing. I do have certain thoughts about it, but first want to see if someone knows.

Here is the query I have built when getting posts by SINGLE tag:

SELECT `wp_posts`.`ID`, `wp_posts`.`post_name`, `wp_posts`.`post_title`, `wp_posts`.`post_date`, `wp_posts`.`post_content` , DATEDIFF(CURDATE(),post_date) as days_ago 
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)
    AND wp_term_taxonomy.taxonomy = 'post_tag'
    AND wp_terms.slug SOUNDS LIKE 'Hennessy'
    AND wp_terms.slug SOUNDS LIKE 'VS' 
    AND wp_posts.post_type = 'post'
    AND (wp_posts.post_status = 'publish')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC


share|improve this question
"Different platform"; you are working with the WordPress database, though? – MikeSchinkel Mar 31 '11 at 16:02
@Mike Yes, I would say MYSQL database. As I am not using Wordpress Database abstraction classes – simple Mar 31 '11 at 16:07
But you are using the WordPress database schema, right? – MikeSchinkel Mar 31 '11 at 16:20
@Mike Yes you are right the Wp db schema – simple Apr 1 '11 at 22:36

You seem to be catching wrong query. Dumping SQL for such URL (via posts_request filter) get me this:

FROM wp_posts  
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) 
WHERE 1=1  
AND ( wp_term_relationships.term_taxonomy_id IN (93) AND tt1.term_taxonomy_id IN (94) AND wp_posts.ID IN (
                    SELECT object_id
                    FROM wp_term_relationships
                    WHERE term_taxonomy_id IN (93,94)
                    GROUP BY object_id HAVING COUNT(object_id) = 2
                ) ) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

In any case see WP_Query->get_posts() method, which handles turning query variables into SQL. There is a lot of code there and a lot of it was changed in 3.1, so it's a little hard for me to pinpoint where multiple tags are handled.

share|improve this answer
Yes you are right I have somewhat, slightly different query, but even from the one you pointed, I am not able to use it for multiple "tag" selection – simple Apr 1 '11 at 22:37
@simple does URL example.com/tag/first+second fetch what you want? Just hook into posts_request and dump query it generates, as I did. Need help with that? – Rarst Apr 1 '11 at 22:40
@Rast it does fetch, but it runs a query similar to "SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (10649,7783,6336,4556,3938)", But what I need is to get those post_id s! I also check them against Soundex, so the case just get's a little funnier! Any thought on how to dump all the queries wordpress executes per one Request ? – simple Apr 2 '11 at 6:34
@simple That query is not even query for posts, it is query for custom fields. I am not sure what it has to do with anything, as above - please try to dump posts_request filter if you need query that is run to retrieve posts. – Rarst Apr 2 '11 at 11:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.