我需要使用自定义选择查询显示一些帖子。此自定义选择查询需要在两个分类中获取帖子。
查看查询:
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID =
$wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy
ON($wpdb->term_relationships.term_taxonomy_id =
$wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id =
$wpdb->terms.term_id)
WHERE
$wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'continentes'
AND $wpdb->terms.slug = 'asia'
AND $wpdb->term_taxonomy.taxonomy = 'pais'
AND $wpdb->terms.slug = 'china'
ORDER BY $wpdb->posts.post_date DESC
如果只使用一个条件,则查询效果很好,如:
... AND $ wpdb-> term_taxonomy.taxonomy ='continentes' AND $ wpdb-> terms.slug ='asia' ...
但我需要分类学“continentes”和“pais”中的帖子。
祝你好运
答案 0 :(得分:0)
...
AND
($wpdb->term_taxonomy.taxonomy = 'continentes'
OR
$wpdb->term_taxonomy.taxonomy = 'pais')
...
如果Wordpress支持它,您可以使用IN
:
...
AND $wpdb->term_taxonomy.taxonomy in ('continentes', 'pais')
...
答案 1 :(得分:0)
假设每个帖子只有1个分类,您的查询将不会返回任何结果,因为帖子不能同时在大陆和pais中。你需要改变你的where子句看起来像这样:
AND ($wpdb->term_taxonomy.taxonomy = 'continentes' OR $wpdb->term_taxonomy.taxonomy = 'pais')