显示至少已发布一次的用户的电子邮件

时间:2015-12-24 作者:Graham Slick

我需要在页面上显示已经在我的网站上发布过一次的用户的电子邮件列表。

我有一段代码:

global $wpdb;
$min_posts = 1; 
$author_ids = $wpdb->get_col("SELECT `post_author` FROM
    (SELECT `post_author`, COUNT(*) AS `count` FROM {$wpdb->posts}
        WHERE `post_status`=\'publish\' GROUP BY `post_author`) AS `stats`
    WHERE `count` >= {$min_posts} ORDER BY `count` DESC;");
但我有两个问题:如何选择在特定页面上显示它(我通常向functions.php添加代码片段),以及如何获取电子邮件而不是ID?

我可能不得不使用get_the_author_meta( \'user_email\' ) 但我不知道如何在这里实现它。

谢谢你的时间和帮助

2 个回复
最合适的回答,由SO网友:Prasad Nevase 整理而成

您可以在函数中放置以下代码。php文件(&P);使用短代码[myblogwriters min_posts="1"] 页面/帖子内容中的任意位置,也可以更改min\\u posts值:)

function show_min_one_post_writers($atts){

    global $wpdb;

    $attrs = shortcode_atts( array(
            \'min_posts\' => \'\'
        ), $atts );

    $min_posts = $attrs[\'min_posts\'];

    $authors = $wpdb->get_col("SELECT `post_author` FROM
    (SELECT `post_author`, COUNT(*) AS `count` FROM {$wpdb->posts}
    WHERE `post_status`=\'publish\' GROUP BY `post_author`) AS `stats`
    WHERE `count` >= {$min_posts} ORDER BY `count` DESC;");

    $my_blog_writers = "";

    if ( empty ($authors) ) {
        return "<p>No one has contirbuted any post yet!</p>";
    } else {
        foreach ($authors as $author_id) {
            $my_writer = get_userdata($author_id);
            $my_blog_writers .= "<p>".$my_writer->first_name ." ".$my_writer->last_name ." : ". $my_writer->user_email."</p>";
        }
        return $my_blog_writers;
    }    
}
add_shortcode( \'myblogwriters\', \'show_min_one_post_writers\' );

SO网友:WPTC-Troop

如果查询正确并返回user_id 然后您可以运行下面的

foreach($user_id_array as $user_id){
  echo get_the_author_meta( \'user_email\', $user_id );
  echo "<br>";//to print in new line for each user mail
}
您可以在页面模板或任何模板挂钩中使用上述内容。例如,您可以在wp_head,wp_footer

让我们在一张空白的纸上做这件事。让我们假设page_id8 然后,下面的过滤器将执行此操作

function print_selected_umail($content){

//check if we are in required page(8), so that we can add the selected user mail
if(is_page(8)){
    //queried result
        foreach($user_id_array as $user_id){
      $content .= get_the_author_meta( \'user_email\', $user_id );
      $content .= "<br>";//to print in new line for each user mail
    }
   }
return $content;
}
add_filter(\'the_content\',\'print_selected_umail\');