轻松的本机SQL查询:
global $wpdb;
$sql = <<<SQL
SELECT post_status, COUNT( * ) AS count
FROM {$wpdb->posts}
WHERE post_status = \'pending\'
SQL;
$result = $wpdb->get_results( $sql );
结果会是
+ ----------- + ----- +
| post_status | count |
+ ----------- + ----- +
| pending | 158 |
+ ----------- + ----- +
因此,您可以使用
$result->count
.
is_user_logged_in()
检查用户是否已登录。
和edit_post_link()
你可以直接链接到这些帖子(在你单独查询之后)。下面将链接到按帖子状态筛选的帖子管理表pending
:
printf( \'<a href="%s">Pending Posts</a>\', admin_url( \'edit.php?post_status=pending\' ) );
插件作为插件:
<?php
/**
* Plugin Name: (#115946) Post Count by status
* Plugin URI: http://wordpress.stackexchange.com/questions/115946
* Author: Franz Josef Kaiser <[email protected]>
* Author URI: http://unserkaiser.com
*/
function wpse115946PostCountByStatus( $status )
{
global $wpdb;
$sql = <<<SQL
SELECT post_status, COUNT( * ) AS count
FROM {$wpdb->posts}
WHERE post_status = %s
SQL;
$result = array_shift( $wpdb->get_results( $wpdb->prepare( $sql, $status ) ) );
$title = sprintf( _e( \'%s %s posts\', ucwords( $status ), $result->count ) );
return printf(
\'<a href="%1$s" title="%2$s">%2$s</a>\',
admin_url( \'edit.php?post_status=pending\' ),
$title
);
}