0

I need to display on a page the list of emails of the users who already posted once on my site.

I have this snippet of code:

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

But I have two issues: how can I choose to display it on a particular page (I usually add snippets to functions.php) and how can I get the emails instead of the iDs ?

I probably have to use get_the_author_meta( 'user_email' ) but I don't know how to implement it here.

Thanks for your time and help

3

You can place following code in your functions.php file & use the shortcode [myblogwriters min_posts="1"] anywhere in the page/post content and can also change the min_posts value :)

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' );
  • 1
    Hi @itsMePN using shortcode is an good idea. so that they can place it anywhere they want easily without much programming. Thums up – WPTC-Troop Dec 24 '15 at 10:20
  • 1
    @itsMePn amazing, very helpful. thank you very much – Graham Slick Dec 24 '15 at 10:32
2

If your query is correct and returns list of user_id then you can run the below

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
}

You can use the above in page template or in any templating hook. For example you can print this in wp_head,wp_footer etc

Let us do this in an empty page. Let us assume the page_id is 8 then the following filter will do the trick

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.