首页 > 解决方案 > Wordpress MySQL 查询帖子类型

问题描述

我想在这个查询中添加 post_type 选项。我该怎么做?

$state = $wpdb->get_results(" SELECT meta_value AS state, COUNT(post_id) AS count FROM {$wpdb->postmeta} WHERE meta_key = 'personal_nation' GROUP BY state ORDER BY state DESC");

标签: sqlwordpress

解决方案


试试这个查询

SELECT meta_value AS state, COUNT(post_id) AS count, post_id 
FROM wp_postmeta WHERE meta_key = 'personal_nation' 
AND post_id in(select ID from wp_posts where post_type="post") 
GROUP BY state ORDER BY state DESC

推荐阅读