Wordpress自定义选择查询 - 两个标签的帖子ID

时间:2012-01-09 10:42:03

标签: mysql wordpress

在Wordpress中,我正在尝试运行一个查询来返回一个postID,它被两个(两个)'标签'标记。我想我可能需要某种子查询。任何帮助,在这里;我到目前为止所得到的。

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' )

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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'