wpdb custom post_type problem

时间:2014-12-11 作者:Wordpress User

我想在自定义SQL查询中使用自定义帖子类型,但是,当我在我的查询中添加自定义帖子类型时,我没有得到任何结果。

我的问题在哪里?

这是我试过的代码

$top5_ov_performance = $wpdb->get_results("SELECT post_id,meta_value FROM ".$wpdb->prefix."postmeta WHERE meta_key = \'sd_top5_overclocing\' AND (post_type = \'cpu\')  ORDER BY meta_value DESC LIMIT 0,5 ");
我也试过这个post_type = \'cpu\' 而不是(post_type = \'cpu\')and 而不是AND

2 个回复
SO网友:Wordpress User

我找到了一种方法:

global $wpdb,$post;
                                    $query_ids = $wpdb->get_results("SELECT ID FROM ".$wpdb->prefix."posts WHERE (post_type = \'cpu\') LIMIT 0,10");
                                        foreach ($query_ids as $query_id) {
                                            $query = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."postmeta WHERE post_id = ".$query_id->ID." AND meta_key = \'sd_rating\' ORDER BY meta_value LIMIT 0,10");
                                                foreach ($query as $value) {
                                                    echo $value->meta_value;
                                                }
                                        }

SO网友:Milo

如果要获取特定post类型中某个键的所有元值,可以在单个查询中执行以下操作:

SELECT post_id, meta_value FROM $wpdb->postmeta pm, $wpdb->posts p
WHERE meta_key = \'sd_rating\' and pm.post_id = p.ID  AND p.post_type = \'cpu\'

结束