2015-03-31 67 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