通过创建自定义sql查询解决。
Request for years:
SELECT
count(*),
$wpdb->posts.id as post_id_ts,
YEAR(
(SELECT
$wpdb->postmeta.meta_value
FROM
$wpdb->postmeta
WHERE
$wpdb->postmeta.post_id = post_id_ts
AND
$wpdb->postmeta.meta_key = \'docs_pubdate\'))
AS
years
FROM
$wpdb->posts
WHERE
$wpdb->posts.post_type = \'document_base\'
AND
$wpdb->posts.post_status = \'publish\'
GROUP BY
years
将返回如下结果:
Array (
[0] => Array (
[count(*)] => 1
[post_id_ts] => 1126
[years] => 2017
)
[1] => Array (
[count(*)] => 3
[post_id_ts] => 1121
[years] => 2019
)
)
Request for months:
SELECT
count(*),
$wpdb->posts.id as post_id_ts,
MONTH(
(SELECT
$wpdb->postmeta.meta_value
FROM
$wpdb->postmeta
WHERE
$wpdb->postmeta.post_id = post_id_ts
AND
$wpdb->postmeta.meta_key = \'docs_pubdate\'))
AS
months
FROM
$wpdb->posts
WHERE
$wpdb->posts.post_type = \'document_base\'
AND
$wpdb->posts.post_status = \'publish\'
GROUP BY
months
返回值为:
Array (
[0] => Array (
[count(*)] => 2
[post_id_ts] => 1121
[month] => 1
)
[1] => Array (
[count(*)] => 1
[post_id_ts] => 1122
[month] => 2
)
[2] => Array (
[count(*)] => 1
[post_id_ts] => 1126
[month] => 6
)
)