2012-01-09 49 views
0

在Wordpress中,我試圖運行一個查詢以返回已由兩個(兩個)「標籤」標記的postID。我想我可能需要某種子查詢。任何幫助表示讚賞,在這裏;我到目前爲止。WordPress的自定義選擇查詢 - 這兩個標籤發佈ID

SELECT wposts .id 
FROM wp_posts wposts 
INNER JOIN wp_term_relationships ON(wposts.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 wposts.post_status = 'publish' AND wposts.post_type = 'header-image' AND wp_term_taxonomy.taxonomy = 'tags' AND (wp_terms.name ='homepage' AND wp_terms.name ='position1') 
+0

請添加RDBMS類型作爲標籤。 – 2012-01-09 11:01:39

+0

ammend。抱歉。 – Paul 2012-01-09 13:03:32

回答

1
SELECT p .id 

FROM wp_posts AS p 
    INNER JOIN wp_term_relationships AS rel1 
    ON p.ID = rel1.object_id 
    INNER JOIN wp_term_taxonomy AS tax1 
    ON rel1.term_taxonomy_id = tax1.term_taxonomy_id 
    INNER JOIN wp_terms AS term1 
    ON tax1.term_id = term1.term_id 

    INNER JOIN wp_term_relationships AS rel2 
    ON p.ID = rel2.object_id 
    INNER JOIN wp_term_taxonomy AS tax2 
    ON rel2.term_taxonomy_id = tax2.term_taxonomy_id 
    INNER JOIN wp_terms AS term2 
    ON tax2.term_id = term2.term_id 

WHERE p.post_status = 'publish' 
    AND p.post_type = 'header-image' 
    AND tax1.taxonomy = 'tags' 
    AND term1.name ='homepage' 
    AND tax2.taxonomy = 'tags' 
    AND term2.name ='position1' 
+0

作品像一種享受。完善。 – Paul 2012-01-10 09:28:51

1

試試這個:

<?php 

// The Query 
$the_query = new WP_Query('tag=TAG1+TAG2'); 

// The Loop 
while ($the_query->have_posts()) : $the_query->the_post();   
    the_title(); 
    the_content(); 
endwhile; 

// Reset Post Data 
wp_reset_postdata(); 

?> 

此處瞭解詳情:http://codex.wordpress.org/Class_Reference/WP_Query

如果你是直接與數據庫中的WP應用工作的:http://codex.wordpress.org/Class_Reference/wpdb

+0

不適合我在插件中試圖做的事情,但無論如何都要有一個upvote。 – Paul 2012-01-10 09:29:15

+0

如果你可以解釋你想要的東西,我可能可以幫助你, – janw 2012-01-10 11:05:22