2015-03-31 91 views
2

我需要幫助來開發wordpress定製。我有以下MySQL查詢與UNION指令:Mysql UNION on wordpress

SELECT 
    wp_posts.* 
FROM wp_posts 
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
    LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
    LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id AND mt1.meta_key = 'dir_featured') 
WHERE 
    (wp_term_relationships.term_taxonomy_id IN (2151)) AND 
    wp_posts.post_type = 'ait-dir-item' AND 
    ((wp_posts.post_status = 'publish')) AND 
    ( 
     ( 
     wp_postmeta.meta_key = 'dir_featured' AND 
     CAST(wp_postmeta.meta_value AS CHAR) = 'yes' 
     ) 
    ) 
GROUP BY wp_posts.ID 
UNION 
SELECT 
    wp_posts.* 
FROM wp_posts 
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
    LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
    LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id AND mt1.meta_key = 'dir_featured') 
WHERE 
    wp_term_relationships.term_taxonomy_id IN (2151) AND 
    wp_posts.post_type = 'ait-dir-item' AND 
    wp_posts.post_status = 'publish' AND 
    mt1.post_id IS NULL 
GROUP BY wp_posts.ID 

我想把這個查詢與WordPress參數轉化。可能嗎?

+4

你是什麼意思「翻譯此查詢與wordpress參數」?你能澄清一下嗎? – Epodax 2015-03-31 12:57:42

+0

我打算把它與WordPress的參數寫入作爲該樣品 \t $ PARAMS =陣列( \t \t 'post_type' \t \t \t> 'AIT-DIR-項', \t \t 'nopaging' \t \t \t => \t真, \t \t 'meta_query'=>數組( \t \t \t '關係'=> 'OR', \t \t \t \t陣列( \t \t \t \t \t '鍵'=> 'dir_featured', \t \t \t \t \t '值'=> '是', \t \t \t \t \t '比較'=> '=' \t \t \t \t) \t \t \t \t陣列( \t \t \t \t \t '鍵'=> 'dir_featured', \t \t \t \t \t '值'=> '', \t \t \t \t \t '比較'=> 'NOT EXISTS' \t \t \t \t) \t \t \t)\t, \t \t '的OrderBy' \t \t \t => ARR AY( 'dir_featured'=> 'DESC', 'POST_DATE'=> 'ASC'), \t \t 'post_status' \t \t => '發佈', \t \t 'tag__in' \t \t \t => $ queried_object - > term_id \t); – PuroSangueZ 2015-03-31 13:58:40

+0

當你需要wp_postmeta的記錄時,爲什麼你要使用左連接到wp_postmeta?爲什麼在第一個查詢中沒有使用wp_meta的第二個實例時,會出現LEFT JOIN? – symcbean 2015-03-31 14:04:27

回答

0

這個查詢的目的是什麼? 這是你在Wordpress模式下的查詢

$args = array(
    'cat'   => 2151, 
    'post_type'  => 'ait-dir-item', 
    'post_status' => 'publish', 
    'meta_query'  => array(
       array(
        'key'  => 'dir_featured', 
        'value' => 'yes', 
        'compare' => '==', 
       ), 
      ), 

); 
query_posts($args); 
+0

你好,謝謝你的回答,但你的參數只對應於第一個查詢,但不要考慮到「UNION」和下一個查詢。我需要翻譯該查詢以顯示某些以某種方式訂購的「贊助」項目。發佈的查詢結果正是我想要實現的。 我認爲我們正在跟蹤...再次感謝您的幫助 – PuroSangueZ 2015-03-31 17:11:08