wordpress查询按自定义字段,标记和类别过滤

时间:2011-12-19 14:58:29

标签: wordpress custom-fields wpdb

我已经通过类别,标记和自定义字段筛选来自WordPress博客过滤的帖子。

SELECT wp_posts.* 
FROM wp_posts
WHERE   wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'

AND     (       SELECT COUNT(*) 
                FROM wp_term_relationships
                LEFT JOIN wp_term_taxonomy ON ( wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
                LEFT JOIN wp_terms ON ( wp_term_taxonomy.term_id = wp_terms.term_id )
                WHERE wp_posts.ID = wp_term_relationships.object_id 
                AND     ( wp_terms.name = 'collaborazioni' && wp_term_taxonomy.taxonomy = 'category' ) 
                        ||
                        ( wp_terms.name = 'jammin' && wp_term_taxonomy.taxonomy = 'post_tag' )
            ) >= 1

AND     (       SELECT COUNT(*) FROM wp_postmeta
                WHERE wp_postmeta.post_id = wp_posts.ID 
                AND wp_postmeta.meta_key = 'Product-code'
                AND wp_postmeta.meta_value = 'xxxxxx'
                ) >= 1

但我认为这有点重..你有更好的解决方案吗?

谢谢,Pietro。

1 个答案:

答案 0 :(得分:0)

你想要缩短什么?它看起来已经很短了。

PS。您可能希望在查询中使用{$wpdb->prefix}而不是wp_