WordPress——统计所有用户的帖子
Posted
技术标签:
【中文标题】WordPress——统计所有用户的帖子【英文标题】:Wordpress- counting posts of all users 【发布时间】:2018-10-04 07:00:50 【问题描述】:我正在为 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>';
【问题讨论】:
$result 应该排列并在赋值时将其用作 $result[] 我只会使用get_users()
和query_posts( 'author=' . $theauthorid );
另外,您的 id 仅从 (int)1 变为 (int)6 - 如果用户已被删除等 - 它会扭曲您的 MySQL
【参考方案1】:
您可以使用count_user_posts
在 wordpress 中获取任何用户帖子。因此,您无需为此编写任何查询。请检查以下链接
https://developer.wordpress.org/reference/functions/count_user_posts/
要获得用户循环,您可以get_users
。https://codex.wordpress.org/Function_Reference/get_users
【讨论】:
【参考方案2】:这里$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
【讨论】:
$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);现在它在表中打印 8 行,没有来自数据库的任何数据;/【参考方案3】:修改了 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>';
【讨论】:
【参考方案4】:如果您需要在自定义查询中获取具有post count的用户,您可以试试这个:
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);
【讨论】:
以上是关于WordPress——统计所有用户的帖子的主要内容,如果未能解决你的问题,请参考以下文章