Esempio di SQL RAW per SELEZIONARE post con 2 o più tag
Ho bisogno di un SQL RAW, poiché sto lavorando su una piattaforma diversa con il database di WordPress.
Quando eseguo example.com/tag/first+second
, WordPress esegue una query SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (10649,7783,6336,4556,3938)
Da dove ha preso questi ID post? Sto solo supponendo. Ho alcune ipotesi al riguardo, ma prima vorrei vedere se qualcuno lo sa.
Ecco la query che ho costruito per ottenere post con un SINGOLO 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)
WHERE 1=1
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
grazie.

Sembra che tu stia intercettando una query errata. Dumpando l'SQL per questo URL (tramite il filtro posts_request
) ottengo questo:
SELECT SQL_CALC_FOUND_ROWS wp_posts.*
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 ogni caso, guarda il metodo WP_Query->get_posts()
, che gestisce la trasformazione delle variabili di query in SQL. C'è molto codice lì e gran parte è stato modificato nella versione 3.1, quindi è un po' difficile per me identificare esattamente dove vengono gestiti i tag multipli.

Sì hai ragione ho una domanda leggermente diversa, ma anche da quella che hai indicato, non riesco a usarla per la selezione multipla di "tag"

@simple l'URL example.com/tag/first+second
recupera quello che vuoi? Basta agganciarsi a posts_request
e scaricare la query che genera, come ho fatto io. Hai bisogno di aiuto con questo?

@Rast sì recupera, ma esegue una query simile a "SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (10649,7783,6336,4556,3938)"
, Ma quello che mi serve è ottenere quei post_id! Li controllo anche contro Soundex, quindi il caso diventa ancora più divertente! Hai qualche idea su come scaricare tutte le query che wordpress esegue per una singola Richiesta?
