WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Most Commented] 2 lists in sidebar? (6 posts)

  1. tydende
    Member
    Posted 4 years ago #

    Hi,

    I'm trying to add two most commented lists to our sidebar, Most Commented This Week, and Most Commented All-Time.

    However, whichever list is second in the sidebar code automatically displays the same results as the first. In other words, the secodn list does not show the most commented posts for the number of days stated in the code, but rather the number of days stated in the first bit of code.

    I'm using
    <?php mdv_most_commented(10, '', '

    ', true, 1000, true); ?>
    for Most Commented All-Time
    and
    <?php mdv_most_commented(10, '', '

    ', true, 7, true); ?>
    fr Most Commented This Week

    Has anyone else been able to show two Most Commented lists or have an idea how to get around this?

    Thanks!

  2. tugbucket
    Member
    Posted 4 years ago #

    Just to make sure I know what you're wanting.

    You want to get the most commented posts where the comment occurred in the last week right?

    If that's right, you can't use the same plug-in your using. That plugin is looking at the posts in the database not the comments. It is looking for the date in which a post was actually posted and then seeing if it had comments.

    The info you want to check is in a different table in the database so you need a totally different connection.

    Try this:

    <h2>Most commented posts from the last 7 days</h2>
    <ul>
    <?php
    $date1 = date('Y-m-d', strtotime('+1 day'));
    $date2 = date('Y-m-d', strtotime('-8 days'));
    $result = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_date BETWEEN '". $date2 ."' AND '" . $date1 . "' GROUP BY comment_post_ID ORDER BY comment_date DESC LIMIT 0 , 10");
    
    foreach ($result as $topten) {
        $postid = $topten->comment_post_ID;
        $commentapproved = $topten->comment_approved;
    
        if ($commentapproved != 0) {
        ?>
             <li><a href="<?php echo get_permalink($postid); ?>"><?php echo get_the_title($postid); ?></a></li>
        <?php }
    }
    ?>
    </ul>

    This appears to list the 10 most commented posts based on if the comment it self was made in the last 7 days (well 8 days actually or it won't count today).

  3. tugbucket
    Member
    Posted 4 years ago #

    Okay this might be better to some extent, it will show the number of comments for the week off to the side.

    <h2>Most commented posts from the last 7 days</h2>
    <ul>
    <?php
    $date1 = date('Y-m-d', strtotime('+1 days'));
    $date2 = date('Y-m-d', strtotime('-8 days'));
    $result = $wpdb->get_results("SELECT comment_date, comment_post_ID, comment_approved, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments WHERE comment_date BETWEEN '". $date2 ."' AND '" . $date1  . "' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID GROUP BY comment_post_ID ORDER BY comment_count DESC LIMIT 0 , 10");
    
    foreach ($result as $topten) {
        $postid = $topten->comment_post_ID;
        $commentapproved = $topten->comment_approved;
    	$commentcount = $topten->comment_count;
        if ($commentapproved != 0) {
        ?>
             <li><a href="<?php echo get_permalink($postid); ?>"><?php echo get_the_title($postid); ?></a> <small>(<?php echo $commentcount.' this week'; ?>)</small></li>
        <?php }
    }
    ?>
    </ul>
  4. tydende
    Member
    Posted 4 years ago #

    Hey tugbucket,

    Thanks for the response! But, actually all we're trying to do is show the most commented posts (as the plugin is designed to work), but show 2 lists based on 2 date ranges. One for the posts that were written in the last 30 days that have the most comments, and one for the psts that were written in the last 1000 days that have the most comments.

    it seems pretty straightforward but will not show the two lists, only the first one queried in the sidebar.

    Does this make sense?

    Thanks again for the response,
    Ty

  5. tugbucket
    Member
    Posted 4 years ago #

    well it looks like you'll need to hack the plugin to include an offset for example you have:

    <?php mdv_most_commented(10, '', '', true, 1000, true); ?>

    and

    <?php mdv_most_commented(10, '', '', true, 7, true); ?>

    the "10" is the number to show. So if you call the plugin the same way twice both having 10 results, you'll get the same 10 results both times.

    So if you're showing the top 10 out of each of the date ranges you're asking for you are basically saying:

    <?php mdv_most_commented(20, '', '', true, 1000, true); ?>

    so basically you need a way to add another option to the string like:

    <?php mdv_most_commented(10, '', '', true, 1000, 30, true); ?>

    Where "30" in the back end would be an offset on days.

    I'm using a popular post code myself not sure if it's the same as your's but here some hacked code to achiebve what you want:

    Put this where you want the result to display:

    <h2>past 30 days</h2>
    <ul>
    <?php most_popular_posts_past30(); ?>
    </ul>
    <h2>past 1000 days</h2>
    <ul>
    <?php most_popular_posts_past1000(); ?>
    </ul>

    Put this in your functions.php:

    /* most_popular_posts_past30 */
    function most_popular_posts_past30($no_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $duration='') {
    global $wpdb;
    $date = date('Y-m-d', strtotime('-1 months'));
    $days = (strtotime($date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24);
    $days = str_replace("-", "", $days);
    $duration = $days;
    $request = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments";
    $request .= " WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";
    if(!$show_pass_post) $request .= " AND post_password =''";
    if($duration !="") { $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date ";
    }
    $request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
    $posts = $wpdb->get_results($request);
    $output = '';
    if ($posts) {
    foreach ($posts as $post) {
    $post_title = stripslashes($post->post_title);
    $comment_count = $post->comment_count;
    $permalink = get_permalink($post->ID);
    if (($comment_count)>1){
    $numcoms = "&nbsp;comments";
    }
    else{
    $numcoms = "&nbsp;comment";
    };
    $output .= $before . '<a href="' . $permalink . '" title="' . $post_title.'">' . $post_title . '<span><small> (' . $comment_count. $numcoms .')</small></span></a>' . $after . "\n";
    }
    } else {
    $output .= $before . "None found" . $after;
    }
    echo $output;
    }
    /* end most_popular_posts_past30 */
    
    /* most_popular_posts_past1000 */
    function most_popular_posts_past1000($no_posts = 5, $before = '<li>', $after = '</li>', $show_pass_post = false, $duration='') {
    global $wpdb;
    $date = date('Y-m-d', strtotime('-1 months'));
    $date2 = date('Y-m-d', strtotime('-1000 days'));
    $days = (strtotime($date2) - strtotime($date)) / (60 * 60 * 24);
    $days = str_replace("-", "", $days);
    $duration = $days;
    $request = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments";
    $request .= " WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";
    if(!$show_pass_post) $request .= " AND post_password =''";
    if($duration !="") { $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date ";
    }
    $request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
    $posts = $wpdb->get_results($request);
    $output = '';
    if ($posts) {
    foreach ($posts as $post) {
    $post_title = stripslashes($post->post_title);
    $comment_count = $post->comment_count;
    $permalink = get_permalink($post->ID);
    if (($comment_count)>1){
    $numcoms = "&nbsp;comments";
    }
    else{
    $numcoms = "&nbsp;comment";
    };
    $output .= $before . '<a href="' . $permalink . '" title="' . $post_title.'">' . $post_title . '<span><small> (' . $comment_count. $numcoms .')</small></span></a>' . $after . "\n";
    }
    } else {
    $output .= $before . "None found" . $after;
    }
    echo $output;
    }
    /* end most_popular_posts_past1000 */

    and here it is working on my test page:

    http://tugbucket.net/list-sample/#mostComm

  6. tydende
    Member
    Posted 4 years ago #

    Wow. Thanks tugbucket!

    I'll check this out as soon as I can and post my results. Thank you for the help!

    Cheers,
    Ty

Topic Closed

This topic has been closed to new replies.

About this Topic