我正在使用我的WordPress简单插件。此插件应计算所有用户帖子并将其打印在表格中。但我有一个问题。我使用for循环从MySQL获取数据,然后将其放入$ result数组。然后我使用foreach循环在HTML表格中打印它们。它无法正常工作,因为only 1 (last) record
打印在表格中(应该是6条记录)。你有什么想法?提前致谢。
global $wpdb;
$result = count_users();
$total_users = $result['total_users'];
for($id = 1;$id<=$total_users;$id++){ //there is 6 users i database
$result = $wpdb->get_results("SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' FROM wp_users INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author WHERE wp_posts.post_type = 'post' AND wp_users.ID = $id AND wp_posts.post_status = 'publish'" , ARRAY_A);
};
echo '<table>';
foreach ($result as $x){
echo'<tr>';
// only one is printed
echo'<td>'.'ID: '. $x['ID']."</td>";
echo'<td>'.'User : '. $x['display_name'].'</td>';
echo'<td>'.'Number of posts :'. $x['Number_of_posts'].'</td>';
echo'</tr>';
}
echo '</table>';
echo '<br>';
您可以使用count_user_posts
在wordpress中获取任何用户帖子。所以你不需要为此编写任何查询。请查看以下链接https://developer.wordpress.org/reference/functions/count_user_posts/
为了让用户循环你canget_users
。
https://codex.wordpress.org/Function_Reference/get_users
这里$result
是一个阵列。当分配一些值应该用作$result[]=$somevalue;
。另外,删除“;”来自for循环的右大括号。
global $wpdb;
$result = count_users();
$total_users = $result['total_users'];
for($id = 1;$id<=$total_users;$id++){ //there is 6 users i database
$result[$id] = $wpdb->get_results("SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' FROM wp_users INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author WHERE wp_posts.post_type = 'post' AND wp_users.ID = $id AND wp_posts.post_status = 'publish'" , ARRAY_A);
}
或者您可以使用此解决方案。希望这对你有所帮助。 Count user posts by user ID, Post type, and Post status
修改SQL字符串以仅使用单个查询检索所有需要的记录。
使用:
SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' FROM wp_users
INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author
WHERE wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'
GROUP BY wp_posts.post_author
$wpdb->get_results()
必须被移出第一个for循环。第一个for循环是不必要的,因此被删除。
更新的代码:
global $wpdb;
$result = count_users();
$total_users = $result['total_users'];
$result = $wpdb->get_results("SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' FROM wp_users
INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author
WHERE wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'
GROUP BY wp_posts.post_author" , ARRAY_A);
echo '<table border="1">';
foreach ($result as $x){
echo'<tr>';
echo'<td>'.'ID: '. $x['ID']."</td>";
echo'<td>'.'User : '. $x['display_name'].'</td>';
echo'<td>'.'Number of posts :'. $x['Number_of_posts'].'</td>';
echo'</tr>';
}
echo '</table>';
echo '<br>';
如果您需要在自定义查询中获取具有帖子计数的用户,您可以尝试这样做:
global $wpdb;
$query = "SELECT users.*, posts.post_count FROM $wpdb->users users
INNER JOIN (SELECT post_author, COUNT(*) as post_count FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY post_author) posts ON (users.ID = posts.post_author)
WHERE users.user_status = 1
ORDER BY posts.post_count DESC
LIMIT 0, 10";
return $wpdb->get_results($query);