• I have a really strange problem, well perhaps its not a problem and im just a noob.

    Im trying to pull posts with the most comments from a certain category.

    I have a category called “Scripts”, Its category 1. I want to pull posts from that category with the most comments from the highest number of comments to lowest, maximum 5 comments.

    I have the following function

    function get_hottopics($limit = 5) {
        global $wpdb, $post;
        $mostcommenteds = $wpdb->get_results("SELECT  $wpdb->posts.ID, post_category, post_title, post_name, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'comment_total' FROM $wpdb->posts LEFT JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE post_category = '1' AND comment_approved = '1' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' GROUP BY $wpdb->comments.comment_post_ID ORDER  BY comment_total DESC LIMIT $limit");
        foreach ($mostcommenteds as $post) {
    			$post_title = htmlspecialchars(stripslashes($post->post_title));
    			$comment_total = (int) $post->comment_total;
    			echo "<li><a href=\"".get_permalink()."\">$post_title&nbsp;<strong>($comment_total)</strong></a></li>";
        }
    }

    The problem is, when I look at phpmyadmin all my posts have a post_category of 0…even though I know I have a post in that “Scripts” category.

    And the funny thing is, this code below in another area of the site works to show the most recent script posts.

    <div id="recententries">
    <span class="toptitle"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/icon-comments.gif" alt="icon comments" class="icons" />Recent Reviews</span>
    <ul class="list2">
    <?php $my_query = new WP_Query("<strong>cat=1</strong>&showposts=5");
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title2('', '...', true, '50') ?></a></li>
    <?php endwhile; ?>
    </ul>
    </div>

    It recognises that cat=1

    If I change the following sql in my hottopics function :

    WHERE post_category = '1'

    to

    WHERE post_category = '0'

    It works of course, but it pulls posts from every category.

    Why are my posts not being stored with a category ID attached, yet I can pull posts with cat=1….

    I know im missing something lol

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter netbuddy34

    (@netbuddy34)

    Anyone got a solution for this one? Should i be using in_category somewhere? Ive tried everything i can think of to get it only to display “Script” items.

    Thread Starter netbuddy34

    (@netbuddy34)

    I also tried the most_commented plugin and tried this.

    <?php if (in_category(‘Scripts’)&&function_exists(‘mdv_most_commented’)) mdv_most_commented(); ?>

    That didnt work either.

    would you be interested in me extending the “most comments” plugin to make use of this function? i’m generally thinking about new features.

    http://wordpress.org/extend/plugins/most-comments/

    just added the feature to show comments from a specific category to my plugin “most comments”. let me know if that works for you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Most commented from certain category’ is closed to new replies.