Wordpress-计算所有用户的帖子

时间:2018-04-24 06:08:43

标签: php mysql wordpress plugins

我正在处理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>';

4 个答案:

答案 0 :(得分:0)

此处$result是一个数组。分配某些值时,应使用$result[]=$somevalue;。另外,删除&#34;;&#34;来自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

答案 1 :(得分:0)

您可以使用count_user_posts在wordpress中获取任何用户帖子。因此,您不需要为此编写任何查询。请查看以下链接 https://developer.wordpress.org/reference/functions/count_user_posts/

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果您需要在自定义查询中获得具有帖子数的用户,则可以尝试以下操作:

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