wordpress-counting所有用户的帖子

5ktev3wc  于 2021-06-24  发布在  Mysql
关注(0)|答案(4)|浏览(295)

我正在为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>';
j13ufse2

j13ufse21#

在这里 $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);
}

或者你可以用这个方法。希望这对你有帮助。按用户id、文章类型和文章状态统计用户文章

kupeojn6

kupeojn62#

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 '';

foreach ($result as $x){
echo'';
echo''.'ID: '. $x['ID']."";
echo''.'User : '. $x['display_name'].'';
echo''.'Number of posts :'. $x['Number_of_posts'].'';
echo'';
}
echo '';
echo '';

vnzz0bqm

vnzz0bqm3#

如果需要在自定义查询中获取具有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);
b5buobof

b5buobof4#

你可以用 count_user_posts 在wordpress中获取任何用户帖子。所以不需要为此编写任何查询。请检查下面的链接https://developer.wordpress.org/reference/functions/count_user_posts/
为了得到用户循环,你可以 get_users .
https://codex.wordpress.org/function_reference/get_users

相关问题