开发者

WordPress - Issue when displaying posts using a custom select query

I need to display some posts using a custom select query. This custom select query needs to get the posts in two taxonomies.

Look the query:

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 $wpd开发者_开发百科b->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

If a just use one condition the query works well, like it:

... AND $wpdb->term_taxonomy.taxonomy = 'continentes' AND $wpdb->terms.slug = 'asia' ...

But I need the posts in taxonomy "continentes" and "pais".

Best regards


...
AND 
($wpdb->term_taxonomy.taxonomy = 'continentes' 
OR 
$wpdb->term_taxonomy.taxonomy = 'pais')
...

If Wordpress supports it you can use IN:

...
AND $wpdb->term_taxonomy.taxonomy in ('continentes', 'pais')
...


Assuming each post only has 1 taxonomy, your query won't return any results, as a post can't be in both continents and pais. you need to change your where clause to look like this:

AND ($wpdb->term_taxonomy.taxonomy = 'continentes' OR $wpdb->term_taxonomy.taxonomy = 'pais')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜