How to – filter comments per date and email address and no. of comments
-
Hello, I would like to award the most commenting people every month.
In my blog people cannot register.I would like to:
– Filter all the comments from day June 1 to June 30;
– Sort ascending/descending per number of comments;
– See the email address of the commenters;In this way I can write to the person that commented the most and send the award.
How can I do that?
Thank you
-
Try it with this:
<?php global $wpdb; $comments = $wpdb->get_results("SELECT comment_author_email FROM $wpdb->comments WHERE comment_approved = '1' AND comment_date >= '2011-06-01' AND comment_date < '2011-06-30'"); if($comments){ foreach($comments as $comment){ $coment_email[] = $comment->comment_author_email; } $best_commenters = array_count_values($coment_email); arsort($best_commenters); foreach($best_commenters as $email => $value){ $comment_author= $wpdb->get_results("SELECT comment_author FROM $wpdb->comments WHERE comment_approved = '1' AND comment_author_email = '".$email."' "); echo '<p>comments: '.$value.'<br />comment author: '.$comment_author[0]->comment_author.'<br />comment email: '.$email.'</p>'; } } else { echo 'there are no comments'; } ?>
Don’t print out the comment author emails publicly. Make a private Post or Page and use conditional tags so only you get to see the results.
Thank you. Should I create a pagexyz.php, paste that code inside, put this php file on my WP root and it should work?
Create a custom page template(duplicate your page.php and rename it) and use this code in stead of the loop. Put this file in your theme folder. Publish a Page with this page template and set it to private. Now when you are logged in you can go to this page and see the results. In your header inside the <head></head> tags u can use:
<?php if(is_page( '42' ) ){ // the page ID echo '<meta name="robots" content="noindex, nofollow" />'; } ?>
(change the page ID to your private Page ID)
This is to stop searchengines indexing this Page.Hello, I do not see any loop on my page.php.
What I see is the following:<?php get_header(); ?> <div class="middle_single"> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post_blog"> <h1><?php the_title(); ?></h1> <div style="clear:both"></div> <div class="entry_blog"> <?php the_content(); ?> </div> <div style="clear:both;"></div> <?php endwhile; ?> <?php endif; ?> </div> </div> <?php include(TEMPLATEPATH . '/sidebar-single.php'); ?> <?php get_footer();?>
replace this with my code:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post_blog"> <h1><?php the_title(); ?></h1> <div style="clear:both"></div> <div class="entry_blog"> <?php the_content(); ?> </div> <div style="clear:both;"></div> <?php endwhile; ?> <?php endif; ?> </div>
Thank you.
Instead of putting `<?php
if(is_page( ’42’ ) ){ // the page ID
echo ‘<meta name=”robots” content=”noindex, nofollow” />’;
}
?>` in the header, can I put it in the robots.txt?Disallow in the robots txt would work the same as noindex, nofollow?
I would like to avoid a new php query :/ My hosting is very often getting CPU throttlingYes you can also put it in the robots.txt
Ok, now my page looks like this:
<?php get_header(); ?> <div class="middle_single"> <?php global $wpdb; $comments = $wpdb->get_results("SELECT comment_author_email FROM $wpdb->comments WHERE comment_approved = '1' AND comment_date >= '2011-06-01' AND comment_date < '2011-06-30'"); if($comments){ foreach($comments as $comment){ $coment_email[] = $comment->comment_author_email; } $best_commenters = array_count_values($coment_email); arsort($best_commenters); foreach($best_commenters as $email => $value){ $comment_author= $wpdb->get_results("SELECT comment_author FROM $wpdb->comments WHERE comment_approved = '1' AND comment_author_email = '".$email."' "); echo '<p>comments: '.$value.'<br />comment author: '.$comment_author[0]->comment_author.'<br />comment email: '.$email.'</p>'; } } else { echo 'there are no comments'; } ?> <?php include(TEMPLATEPATH . '/sidebar-single.php'); ?> <?php get_footer();?>
however I am getting the following error:
Fatal error: Call to undefined function get_header() in /home/me/public_html/mysite/wp-content/themes/mytheme/thenewpage.php on line 1
Second question… how can I make this page private so that even knowing the direct URL, only the logged in admin can access it?
Is the file in your theme folder? Is this the whole file? Did you make a custom page template with this at the top:
<?php /* Template Name: newpage */ ?>
Second question… how can I make this page private so that even knowing the direct URL, only the logged in admin can access it?
In the same panel as the “Publish” button you can set a page Visability to “Private”
Excellent!!! Thank you!! It works ??
Pity that I don’t have the white background… I have black text on violet background.I was making the error and trying to enter mysite.com/wp-content/themes/mytheme/newpage.php instead of simply mysite.com/newpage.php
At this point I am wondering if in the robots.txt I have to put the whole path to the file or only the permalink mysite.com/newpage.php
One more thing. I understand that if I want to include the comments done in day 30 I have to change:
comment_date >= '2011-06-01' AND comment_date < '2011-06-30'
to
comment_date >= '2011-06-01' AND comment_date <= '2011-06-30'
?
Pity that I don’t have the white background…
try this. Put this above my code:
<div class="post_blog">
and this after it:</div>
At this point I am wondering if in the robots.txt I have to put the whole path to the file or only the permalink mysite.com/newpage.php
I don’t know much about robots.txt. Google it.
One more thing. I understand that if I want to include the comments done in day 30 I have to change:
try this:
comment_date = '2011-06-01'
(not tested)Hello, I have tried but I still have violet background/no background.
Please look at my code right now:
<?php /* Template Name: topcommenters */ ?> <?php get_header(); ?> <div class="middle_single"> <div class="post_blog"> <?php global $wpdb; $comments = $wpdb->get_results("SELECT comment_author_email FROM $wpdb->comments WHERE comment_approved = '1' AND comment_date >= '2011-06-01' AND comment_date < '2011-07-01'"); if($comments){ foreach($comments as $comment){ $coment_email[] = $comment->comment_author_email; } $best_commenters = array_count_values($coment_email); arsort($best_commenters); foreach($best_commenters as $email => $value){ $comment_author= $wpdb->get_results("SELECT comment_author FROM $wpdb->comments WHERE comment_approved = '1' AND comment_author_email = '".$email."' "); echo '<p>comments: '.$value.'<br />comment author: '.$comment_author[0]->comment_author.'<br />comment email: '.$email.'</p>'; } } else { echo 'there are no comments'; } ?> </div> <?php include(TEMPLATEPATH . '/sidebar-single.php'); ?> <?php get_footer();?>
Oh, and I can also see that with or without the <div class=”post_blog”> the sidebar is broken. The widgets instead of appearing on the sidebar, are appearing under the page.
Try it with this: https://pastebin.com/AjJKuXJ2
- The topic ‘How to – filter comments per date and email address and no. of comments’ is closed to new replies.