自定义按元键和cat id选择查询

时间:2014-11-11 10:56:51

标签: php sql wordpress codex

我有这个自定义查询,获取帖子的过期日期和订单发布。如何将其修改为仅列出某个类别ID ,例如。 ' 20'

$querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = 'es_ape_expiry'
    ORDER BY wpostmeta.meta_value ASC
    ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试以下查询并将$ cat_id替换为20

SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
JOIN $wpdb->terms t ON (tt.term_id = t.term_id)
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'es_ape_expiry' 
AND tt.taxonomy = 'category'
AND t.term_id = $cat_id
ORDER BY wpostmeta.meta_value ASC