Core在这里帮不了你。你必须做一个…
自定义查询
这是一个保存查询,我在管理UI屏幕上使用它来获取在自定义分页中使用它们的帖子的月份总数。请注意,我不仅查询已发布的帖子,还考虑到可能存在一些限制集,然后应用它。
$post_status = esc_attr( $_GET[\'post_status\'] );
$post_status = in_array( $post_status, $GLOBALS[\'avail_post_stati\'] )
? " AND post_status = {$post_status}"
: \'all\'
;
\'all\' === $post_status AND $post_status = \'\';
$total_page_dates = $wpdb->get_results( $wpdb->prepare( "
SELECT
YEAR( post_date ) AS year,
MONTH( post_date ) AS month,
count( ID ) AS posts
FROM {$wpdb->posts}
WHERE
post_type = %s
%s
GROUP BY
YEAR( post_date ),
MONTH( post_date )
ORDER BY post_date
ASC
", get_current_screen()->post_type, $post_status ) );
然后您就可以检查您的结果了
// Inspect the result
var_dump( $total_page_dates );
可能如下所示:
array (size=4)
0 =>
object(stdClass)[1847]
public \'year\' => string \'2013\' (length=4)
public \'month\' => string \'6\' (length=1)
public \'posts\' => string \'19\' (length=2)
1 =>
object(stdClass)[1846]
public \'year\' => string \'2013\' (length=4)
public \'month\' => string \'7\' (length=1)
public \'posts\' => string \'17\' (length=2)
2 =>
object(stdClass)[1845]
public \'year\' => string \'2013\' (length=4)
public \'month\' => string \'8\' (length=1)
public \'posts\' => string \'8\' (length=1)
3 =>
object(stdClass)[1844]
public \'year\' => string \'2013\' (length=4)
public \'month\' => string \'9\' (length=1)
public \'posts\' => string \'2\' (length=1)
然后,您可以循环遍历它,或者简单地获取第一个或最后一个数组项以获得范围。计数-
count( $total_page_dates )
- 将告诉您得到了多少个月,等等。请记住,每个数组值都是
object
, 所以你必须这样访问它
$posts_in_first_month = total_page_dates[0]->posts;