gpt4 book ai didi

php - WordPress-统计所有用户的帖子

转载 作者:行者123 更新时间:2023-11-30 21:44:38 25 4
gpt4 key购买 nike

我正在开发我的 WordPress 简单插件。这个插件应该计算所有用户的帖子并将它们打印在表格中。但我有一个问题。我使用 for 循环从 MySQL 中获取数据,然后将其放入 $result 数组。然后我使用 foreach 循环将它们打印在 HTML 表中。它无法正常工作,因为 only 1 (last) record is printed in the table (should be 6 records).你有什么想法?提前致谢。

 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/

要获取用户循环,您可以get_users
https://codex.wordpress.org/Function_Reference/get_users

关于php - WordPress-统计所有用户的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49994518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com