最简单的方法是使用自定义MySQL查询WPDB
班
global $wpdb;
$table = $wpdb->prefix . \'posts\';
$sql = "SELECT DATE(post_date) AS date, COUNT(ID) AS count
FROM {$table} WHERE post_type = \'my_post_type\' AND post_status = \'publish\' GROUP BY DATE(post_date)";
$rows = $wpdb->get_results($sql);
或者,如果要防止SQL注入,可以使用以下预处理语句:
global $wpdb;
$sql = $wpdb->prepare(
"SELECT DATE(post_date) AS date, COUNT(ID) AS count
FROM %s WHERE post_type = %s AND post_status = \'publish\' GROUP BY DATE(post_date)",
array(
$wpdb->prefix . \'posts\',
\'my_post_type\'
)
);
$rows = $wpdb->get_results($sql);
然后可以迭代
$rows
显示数据。
foreach ($rows as $row) {
echo $row->date . \' -- \' . $row->count;
}