Domanda

Questa è una domanda molto specifica in materia di MySQL come è implementato in WordPress.

Sto cercando di sviluppare un plugin che mostra (selezionare) i post che sono specifici 'tag"e appartengono a specifiche 'categorie'(sia di più)

Mi è stato detto che è impossibile, perché il modo in cui le categorie e i tag sono memorizzati:

  1. wp_posts contiene un elenco di post, ogni post ha un "ID"
  2. wp_terms contiene un elenco di termini (entrambe le categorie e tag).Ogni termine ha un TERM_ID
  3. wp_term_taxonomy dispone di un elenco di termini con il loro TERM_IDs e ha una Tassonomia definizione per ciascuno di essi (una Categoria o Tag)
  4. wp_term_relationships ha le associazioni tra termini e post

Come posso unire le tabelle per ottenere tutti i post con tag "Nucleare" e "Offerte" che appartengono alla categoria "Categoria 1"?

È stato utile?

Soluzione

Ho frainteso tu.Ho pensato che si voleva Nucleare o Offerte speciali.Il seguente dovrebbe dare solo il Nucleare e Offerte.

select p.*
from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id

and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id

and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id

and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')

Altri suggerimenti

Cosa lordo DB struttura.

Comunque, mi piacerebbe fare qualcosa di simile a questo (nota preferisco ESISTE su join, ma si può ri-scrivere come si unisce, se ti piace;la maggior parte delle query analizzatori crollerà il piano di query in ogni caso).Si può fare un po ' di ulteriori giocoleria in un modo o in un altro per farlo funzionare...

SELECT *
  FROM wp_posts p
 WHERE EXISTS( SELECT *
                 FROM wp_term_relationship tr
                WHERE tr.object_id = p.id
                  AND EXISTS( SELECT *
                                FROM wp_term_taxonomy tt
                               WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                 AND tt.taxonomy         = 'category'
                                 AND EXISTS( SELECT *
                                               FROM wp_terms t
                                              WHERE t.term_id = tt.term_id
                                                AND t.name    = "Category1" 
                                           )
                            )
                  AND EXISTS( SELECT *
                                FROM wp_term_taxonomy tt
                               WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                 AND tt.taxonomy         = 'post_tag'
                                 AND EXISTS( SELECT *
                                               FROM wp_terms t
                                              WHERE t.term_id = tt.term_id
                                                AND t.name    = "Nuclear" 
                                           )
                                 AND EXISTS( SELECT *
                                               FROM wp_terms t
                                              WHERE t.term_id = tt.term_id
                                                AND t.name    = "Deals" 
                                           )
                            )
            )

Così ho provato entrambe le opzioni sul mio WordPress db.Ho guardato per la categoria "Tecnologia" nel mio post con il tag "Perl" E "Programmazione".

Eric ha lavorato una volta ho aggiunto una virgola mancante iniziale di istruzione select.Ha restituito 3 record.Il problema è che la sezione che è alla ricerca del "post_tag" è in realtà di lavoro come O opzione.Uno dei miei post era solo un tag non sia.Inoltre sarebbe bene fare il SELECT DISTINCT.

Ho provato Matt versione, ma ha mantenuto la restituzione di un insieme vuoto.Posso provare a "destreggiarsi" con esso.

Prova questo:

select p.*
from wp_posts p, 
wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

where p.id = tr.object_id
and t.term_id = tt.term_id
and tr.term_taxonomy_id = tt.term_taxonomy_id

and p.id = tr2.object_id
and t2.term_id = tt2.term_id
and tr2.term_taxonomy_id = tt2.term_taxonomy_id

and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))

Essenzialmente io sto impiegando 2 copie delle relative tabelle figlio - termini, term_taxonomy, e term_relationship.Una copia si applica la 'Categoria 1' restrizione, l'altro del 'Nucleare' o 'Offerte' di restrizione.

BTW, che tipo di progetto è presente con i messaggi sul nucleare offerte?Cercando di tirarci su qualche lista governativa?;)

Grazie @Eric funziona!Solo un paio di codice correzioni di riferimento per il futuro:

  • la prima select manca coma dopo wp_term_relationship tr2
  • Nella stessa selezionare statemt devono essere le seguenti cambiamenti:
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

tr2

dovrebbe essere

wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

tr3

Davvero così grande risposta ..mi ha aiutato molto..

grande bcoz., mi ha dato un approccio di base per costruire la mia query complessa !

una piccola correzione, pronto per utenti come me :)

"wp_term_relationship" darà "non esiste errore' ..utilizzare wp_term_relationships come è corretto nome di tabella.

Grazie Eric

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top