問題描述
我試圖找出如何掛鈎到/wp-admin/users.php 管理頁面來創建自定義列,以顯示用户在 WPHonors.com 上的自定義帖子類型的帖子數。
我為此創建了一個 trac ticket,但是 @nacin 解釋了為什麼這是一個更適合插件的工作。
我一直無法找到操作用户表的輸出的方法,所以我可以為每個用户添加自定義列 CPT 的帖子計數。這可能與 @nacin 問的問題有關,帖子數量將鏈接到什麼。對於用户現在的’post’ 帖子數,它鏈接到帖子管理頁面,顯示該用户的所有帖子 (/wp-admin/edit.php?author=%author_id%) 。
如果我把它鏈接到某個地方,那就是:
/wp-admin/edit.php?post_type=%post_type%&author=%author_id%
如果這樣甚至有可能,我猜。但是我甚至不需要將它鏈接到任何地方。我主要想要顯示每個人的 CPT 帖子數,擁有 600 用户和 300+的 4 自定義帖子類型的總和。管理員只能提交'post'的帖子,因此用户頁面中的列是無用的。
最佳解決方案
這是 Mike 的教程答案的擴展。我添加了列出的類型的鏈接,所以你可以點擊一個,並被正確地列為該類型的所有帖子的作者,這需要一個額外的變量為 $counts 和一些額外的輸出為 $custom_column[]
add_action('manage_users_columns','yoursite_manage_users_columns');
function yoursite_manage_users_columns($column_headers) {
unset($column_headers['posts']);
$column_headers['custom_posts'] = 'Assets';
return $column_headers;
}
add_action('manage_users_custom_column','yoursite_manage_users_custom_column',10,3);
function yoursite_manage_users_custom_column($custom_column,$column_name,$user_id) {
if ($column_name=='custom_posts') {
$counts = _yoursite_get_author_post_type_counts();
$custom_column = array();
if (isset($counts[$user_id]) && is_array($counts[$user_id]))
foreach($counts[$user_id] as $count) {
$link = admin_url() . "edit.php?post_type=" . $count['type']. "&author=".$user_id;
// admin_url() . "edit.php?author=" . $user->ID;
$custom_column[] = "t<tr><th><a href={$link}>{$count['label']}</a></th><td>{$count['count']}</td></tr>";
}
$custom_column = implode("n",$custom_column);
if (empty($custom_column))
$custom_column = "<th>[none]</th>";
$custom_column = "<table>n{$custom_column}n</table>";
}
return $custom_column;
}
function _yoursite_get_author_post_type_counts() {
static $counts;
if (!isset($counts)) {
global $wpdb;
global $wp_post_types;
$sql = <<<SQL
SELECT
post_type,
post_author,
COUNT(*) AS post_count
FROM
{$wpdb->posts}
WHERE 1=1
AND post_type NOT IN ('revision','nav_menu_item')
AND post_status IN ('publish','pending', 'draft')
GROUP BY
post_type,
post_author
SQL;
$posts = $wpdb->get_results($sql);
foreach($posts as $post) {
$post_type_object = $wp_post_types[$post_type = $post->post_type];
if (!empty($post_type_object->label))
$label = $post_type_object->label;
else if (!empty($post_type_object->labels->name))
$label = $post_type_object->labels->name;
else
$label = ucfirst(str_replace(array('-','_'),' ',$post_type));
if (!isset($counts[$post_author = $post->post_author]))
$counts[$post_author] = array();
$counts[$post_author][] = array(
'label' => $label,
'count' => $post->post_count,
'type' => $post->post_type,
);
}
}
return $counts;
}
次佳解決方案
假設我明白了這個問題,你需要做的是將兩個掛鈎相關聯的列標題和列值作為管理員管理頁面。它們是'manage_{$type}_columns'和'manage_{$type}_custom_column',其中您的 use-case {$type}是 users 。
'manage_users_columns'鈎
第一個很簡單,它允許您指定列標題,從而指定可用列。 WordPress 硬編碼”Posts” 列的值,因為您想要更改它,我們將要使用 unset()刪除它,然後添加一個新的列與相同的標題,而是具有標識符'custom_posts':
add_action('manage_users_columns','yoursite_manage_users_columns');
function yoursite_manage_users_columns($column_headers) {
unset($column_headers['posts']);
$column_headers['custom_posts'] = 'Posts';
return $column_headers;
}
'manage_users_custom_column'鈎
接下來,您需要使用僅用於 non-standard 列的'manage_users_custom_column'掛鈎。我們測試 $column_name=='custom_posts',以使我們的代碼穩健,以防將來添加新的用户列,然後從我寫的_yoursite_get_author_post_type_counts()函數中獲取用户的帖子類型計數,我將在下面討論。然後我用幾種方法來格式化,但是決定一個 HTML <table> 是最合適的 (因為它是一個數據表) 。如果一張桌子不適合你,我會假設你可以很容易地生成不同的標記:
add_action('manage_users_custom_column','yoursite_manage_users_custom_column',10,3);
function yoursite_manage_users_custom_column($custom_column,$column_name,$user_id) {
if ($column_name=='custom_posts') {
$counts = _yoursite_get_author_post_type_counts();
$custom_column = array();
if (isset($counts[$user_id]) && is_array($counts[$user_id]))
foreach($counts[$user_id] as $count)
$custom_column[] = "t<tr><th>{$count['label']}</th>" .
"<td>{$count['count']}</td></tr>";
$custom_column = implode("n",$custom_column);
}
if (empty($custom_column))
$custom_column = "No Posts!";
else
$custom_column = "<table>n{$custom_column}n</table>";
return $custom_column;
}
根據每個用户/作者的帖子類型獲取帖子數
最後是由作者/用户通過帖子類型檢索帖子計數。一般來説,我嘗試使用 WP_Query()在帖子上運行查詢時堅持使用,但是這個查詢將需要使用這麼多其他鈎子,它似乎更容易被”naughty” 所有。
我省略了 $post->post_type 的任何帖子是'revision'或'nav_menu_item',但留在'attachments'中。您可能會更好地明確地包含您想要的帖子類型,而不是排除我所做的幾個。
我也用 $post->post_status 過濾'publish'和'pending'。如果您還要包括'future','private'和/或'draft',則需要在代碼中進行更改。
對於每個頁面加載,我只會調用這個_yoursite_get_author_post_type_counts()函數一次,然後存儲到靜態變量中,而不是為每個用户調用。我存儲在由作者/用户 ID 索引的數組中,該數組包含元素'label'中的 Post Type 名稱的數組,當然也可以在 same-named 元素中計數:
function _yoursite_get_author_post_type_counts() {
static $counts;
if (!isset($counts)) {
global $wpdb;
global $wp_post_types;
$sql = <<<SQL
SELECT
post_type,
post_author,
COUNT(*) AS post_count
FROM
{$wpdb->posts}
WHERE 1=1
AND post_type NOT IN ('revision','nav_menu_item')
AND post_status IN ('publish','pending')
GROUP BY
post_type,
post_author
SQL;
$posts = $wpdb->get_results($sql);
foreach($posts as $post) {
$post_type_object = $wp_post_types[$post_type = $post->post_type];
if (!empty($post_type_object->label))
$label = $post_type_object->label;
else if (!empty($post_type_object->labels->name))
$label = $post_type_object->labels->name;
else
$label = ucfirst(str_replace(array('-','_'),' ',$post_type));
if (!isset($counts[$post_author = $post->post_author]))
$counts[$post_author] = array();
$counts[$post_author][] = array(
'label' => $label,
'count' => $post->post_count,
);
}
}
return $counts;
}
結果 UI
這就是它應用於我的測試安裝的 WordPress 3.0.1 它的樣子:
下載完整的代碼
您可以從 Gist 下載完整的代碼:
-
wp-post-counts-for-user-by-post-type.php
您可以將此代碼複製到主題的 functions.php 文件中,或將該文件存儲在插件中,以您選擇的為準。
希望這可以幫助!
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
