获取文章总数应该使用WordPress API中wp_count_posts()函数,通常能使用API获取的信息,就不要直接去查询,除非使用API实现资源消耗太大。
1
2
3
4
5
6
7
8
9
|
$count_posts
= wp_count_posts();
$published_posts
=
$count_posts
->publish;
//已发布文章的总数
$future_posts
=
$count_posts
->future;
//定时发布的文章(尚未发布)总数
$draft_posts
=
$count_posts
->draft;
//草稿总数
$pending_posts
=
$count_posts
->pending;
//等待复审的文章的总数
$private_posts
=
$count_posts
->
private
;
//私密文章的总数
$trashed_posts
=
$count_posts
->trash;
//回收站中文章的总数
$auto_draft
=
$count_posts
->{
'auto-draft'
};
//自动草稿总数
$inherit
=
$count_posts
->inherit;
//文章修订版(revisions)的总数
|
关于自动草稿,可以阅读这篇文章。
如果统计时想剔除某些分类下的文章,可以这样,假设计算时不想算ID为5的分类下的文章,注意,如果该分类还有子分类,那么子分类也将被剔除,代码如下。
1
2
|
$total_posts
=
new
WP_Query(
"posts_per_page=1&cat=-5"
);
echo
'文章总数:'
.
$total_posts
->found_posts;
|
或者直接用SQL查询语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
global
$wpdb
;
$exclude_ids
=
'5'
;
$sql
= << SELECT
COUNT
(*)
FROM {
$wpdb
->posts}
WHERE 1=1
AND post_type =
'post'
AND ( post_status =
'publish'
OR post_status =
'private'
)
AND {
$wpdb
->posts}.ID NOT IN (
SELECT object_id
FROM
$wpdb
->term_relationships
WHERE term_taxonomy_id IN (
SELECT term_taxonomy_id
FROM
$wpdb
->term_taxonomy
WHERE taxonomy =
'category'
AND ( term_id IN( {
$exclude_ids
} ) OR parent IN ( {
$exclude_ids
} ) )
)
)
SQL;
$numposts
=
$wpdb
->get_var(
$sql
);
echo
'文章总数:'
.
$numposts
;
|
这两种方法消耗资源都不多。
方法与剔除分类相同,例如计算时剔除ID为66的标签下的所有文章
1
2
|
$total_posts
=
new
WP_Query(
array
(
'tag__not_in'
=>
array
( 66 ),
'posts_per_page'
=> 1 ));
echo
'文章总数:'
.
$total_posts
->found_posts;
|
1
2
|
$total_posts
=
new
WP_Query(
array
(
'posts_per_page'
=> 1,
'post__in'
=> get_option(
'sticky_posts'
) );
echo
'置顶文章总数:'
.
$total_posts
->found_posts;
|
1
2
3
|
$today
=
getdate
();
$total_posts
=
new
WP_Query(
'year='
.
$today
[
"year"
] .
'&monthnum='
.
$today
[
"mon"
] .
'&day='
.
$today
[
"mday"
] .
'&posts_per_page=1'
);
echo
'今日发布的新文章数:'
.
$total_posts
->found_posts;
|
1
2
3
4
|
$count_posts
= wp_count_posts(
'page'
);
$published_posts
=
$count_posts
->publish;
//已发布页面的总数
...
//可以获取的信息与post总数相同,请参考第一段代码
|
wp_count_posts()第一个参数是post_type,post_type既可以是post、page,也可以是用户自定义的文章类型,要获取某个custom post type总数,只要传递custom post type的名称即可。例如,要获取名为movie的custom post type总数
1
|
$count_posts
= wp_count_posts(
'movie'
);
|
可以获取的信息与post相同,请参考第一段代码
使用wp_count_comments()函数
1
2
3
4
5
6
7
|
$comments_count
= wp_count_comments();
echo
"全站评论统计信息
;
echo
"待审核的评论: "
.
$comments_count
->moderated .
"条
;
echo
"审核通过的评论: "
.
$comments_count
->approved .
"条
;
echo
"垃圾评论: "
.
$comments_count
->spam .
"条
;
echo
"被丢进回收站的评论: "
.
$comments_count
->trash .
"条
;
echo
"评论总数(不包括垃圾评论): "
.
$comments_count
->total_comments .
"
;
|
注意最后一条评论总数,是除了垃圾评论以外的总数,如果要计算包含垃圾评论的总数,使用下面的SQL语句
1
|
echo
$wpdb
->get_var(
"SELECT COUNT(*) FROM $wpdb->comments"
);
|
如果在functions中使用$wpdb,不要忘记写上
1
|
global $wpdb;
|
使用count_users()函数获取
1
2
3
4
5
6
7
|
$result
= count_users();
echo
'用户总数:'
.
$result
[
'total_users'
] .
'
;
echo
'管理员:'
.
$result
[
'avail_roles'
][
'administrator'
] .
'
;
echo
'编辑:'
.
$result
[
'avail_roles'
][
'editor'
] .
'
;
echo
'作者:'
.
$result
[
'avail_roles'
][
'author'
] .
'
;
echo
'投稿者:'
.
$result
[
'avail_roles'
][
'contributor'
] .
'
;
echo
'订阅:者'
.
$result
[
'avail_roles'
][
'subscriber'
] .
'
;
|
使用wp_count_terms()函数
1
|
echo
'分类总数:'
. wp_count_terms(
'category'
);
|
1
|
echo
'标签总数:'
. wp_count_terms(
'post_tag'
);
|
要获取所有链接的数目,无论属于哪个分类,可以
1
2
|
$total_links
=
$wpdb
->get_var(
"SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"
);
echo
$total_links
;
|
要获取某个分类下的链接总数,假设该分类的ID为2
1
2
|
$total_links = get_term(
2
,
'link_category'
) ;
echo $total_links;
|
所不同的是用SQL语句可以排除私密链接,get_term返回的是所有链接总数,包括私密链接。
转自http://www.solagirl.net/wordpress-site-status.html