2013-03-02 9 views
5

Sto usando WP Data Tables per creare una tabella dal DB SQL. Nel wordpress backend il codice di esempio per utilizzare sembrava questa:Query SQL per il tipo di post personalizzato e più campi personalizzati

SELECT post_id, post_date 
FROM wp_posts 
WHERE post_type = 'custom_post_type' 
AND post_status = 'publish' 

Im cercando di ottenere i valori dei campi personalizzati dal post meta. Ecco quello che ho finora ...

SELECT post_id, post_date 
FROM wp_posts 
WHERE post_type = 'custom_post_type' 
AND post_status = 'publish' 
AND SELECT custom_field_key_1, custom_field_key_2, custom_field_key_3 
FROM wp_postmeta 
WHERE post_id = post_id 

UPDATE:

ho scoperto che p.ID era necessaria, invece di post_id e che ho bisogno cercare il meta_key. Qualcosa di simile ...

SELECT p.post_title, 
     p.post_date, 
     pm.meta_key = 'custom_field_key' 
FROM wp_posts p 
INNER JOIN wp_postmeta pm 
ON p.ID = pm.post_id 
WHERE p.post_type = 'custom_post_type' 
AND p.post_status = 'publish' 
+0

Credo che tu abbia bisogno di "UNIRE" qui. –

risposta

0

Supponendo che SQL standard è supportato avrete bisogno di qualcosa di simile (non testata):

SELECT w.post_id, w.post_date, m.custom_field_key_1, m.custom_field_key_2, m.custom_field_key_3 
FROM wp_posts w, wp_postmeta m 
WHERE post_type = 'custom_post_type' AND post_status = 'publish' 
AND w.post.id = m.post.id 
4

Utilizzare un INNER JOIN:

SELECT p.post_id, 
     p.post_date, 
     pm.custom_field_key_1, 
     pm.custom_field_key_2, 
     pm.custom_field_key_3 
FROM wp_posts p 
    INNER JOIN wp_postmeta pm 
     ON p.post_id = pm.post_id 
WHERE p.post_type = 'custom_post_type' 
    AND p.post_status = 'publish' 
+0

Questo tipo di lavori e ho incluso un aggiornamento. – user2106176

+0

@ user2106176 - contento che questo abbia aiutato. Guardando il tuo aggiornamento, penso che devi spostare "pm.meta_key = 'custom_field_key'" nella tua clausola WHERE. Non è possibile impostare un valore di campo nell'istruzione SELECT. Buona fortuna! – sgeddes

+0

La modifica di p.post_id in p.ID ha funzionato per me. –

0

si può provare questo .

SELECT p.post_id, p.post_date, 
    pm.custom_field_key_1, pm.custom_field_key_2, pm.custom_field_key_3 
FROM wp_posts p 
JOIN wp_postmeta pm ON p.post_id = pm.post_id 
WHERE p.post_type = 'custom_post_type' 
AND p.post_status = 'publish' 
0

Ok anche se hai aggiornato una risposta. Ho seguito il tuo esempio e ho lavorato questo fuori:

SELECT p.ID, 
     p.post_title, 
     pm.meta_value as 'value1', 
     pma.meta_value as 'value2' 

FROM wp_posts p 
     INNER JOIN wp_postmeta AS pm ON pm.post_id = p.ID 
     INNER JOIN wp_postmeta AS pma ON pma.post_id = p.ID 

WHERE 
     pma.meta_key = 'custom_field_key_1' AND 
     pm.meta_key = 'custom_field_key_2' AND 

     p.post_type = 'your_post_type' AND 
     p.post_status = 'publish' 

Così qui sto usando alias AS per i valori ospitati nella stessa tabella, INNER JOIN per wp_postmeta e post_id e questo è tutto.

Riferimenti: Multiple Inner join same table

In questo modo si otterrà un allineamento con i messaggi e le vostre campi personalizzati selezionati:

array(1) { 
    [0]=> object(stdClass)#341 (4) { 
      ["ID"]=> string(1) "1123" 
      ["post_title"]=> string(15) "Your post title" 
      ["custom_field_key_1"]=> string(12) "Your value 1 " 
      ["custom_field_key_2"]=> string(29) "Your value 2" 
} 

È possibile aggiungere il numero di alias e meta_key di cui hai bisogno. Spero che questo aiuto!

Problemi correlati