我有一个自定义的帖子状态,问题是它的过滤器选项会自动显示在编辑的帖子列表上方。WP admin的php页面显示了具有此状态的所有帖子的总数。我希望它显示特定于相关用户的计数。
比如说
所有(1)|已发布(0)|草稿(0)|挂起(0)| |垃圾(0)|Awaiting (8)
等待的数字8是系统中所有状态为的帖子,但我登录的用户,如果他们没有设置为该状态的帖子,我希望该状态显示为零或根本不可见(我相信WP中的默认功能)。
我看不出这是否是WP支持的功能,但我认为必须有一种方法,因为过滤器链接中列出的其他总计是特定于用户的。
这是通过简单的register\\u post\\u status功能进行的所有设置。。。。
function awaiting_custom_post_status(){
register_post_status( \'awaiting\', array(
\'label\' => _x( \'Changes Awaiting Approval\', \'apartments\' ),
\'public\' => true,
\'exclude_from_search\' => false,
\'show_in_admin_all_list\' => true,
\'show_in_admin_status_list\' => true,
\'label_count\' => _n_noop( \'Changes Awaiting Approval <span class="count">(%s)</span>\', \'Changes Awaiting Approval <span class="count">(%s)</span>\' ),
) );
}
add_action( \'init\', \'awaiting_custom_post_status\' );
有人知道如何将此计数值修改为特定于用户的值,如其他值吗?
最合适的回答,由SO网友:jimihenrik 整理而成
编辑:我敢肯定,这是原作的功劳[this] 就是我从那里抓到的。似乎快速搜索显示stackexchange对相同/ish代码进行了大量修改。但由于评论很到位,我想说所有的功劳都归于“W van Dam”
我有一个类似的设置,多个用户只看到自己的帖子,但我根本没有使用自定义帖子状态。
这是一个旧项目的旧代码片段,但可能有一些用处
add_filter(\'wp_count_posts\', function($counts, $type, $perm) {
global $wpdb;
// We only want to modify the counts shown in admin and depending on $perm being \'readable\'
if (!is_admin() || \'readable\' !== $perm) return $counts;
// Only modify the counts if the user is not allowed to edit the posts of others
$post_type_object = get_post_type_object($type);
if (current_user_can( $post_type_object->cap->edit_others_posts ) ) { return $counts; }
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND (post_author = %d) GROUP BY post_status";
$results = (array) $wpdb->get_results($wpdb->prepare( $query, $type, get_current_user_id() ), ARRAY_A);
$counts = array_fill_keys(get_post_stati(), 0);
foreach ($results as $row) { $counts[ $row[\'post_status\'] ] = $row[\'num_posts\']; }
return (object) $counts;
}, 10, 3);
我之所以询问他们看到的帖子,是因为我确信这将编辑计数,只显示他们自己的帖子。