views:

59

answers:

1

I have a Wordpress website in which one of the categories I've stripped the comments field down to basic whole numbers only. The logged in user can enter data on the post via the "comments" field, in basic simple whole numbers. Is there any way to take the MOST RECENT comment on all posts from that category (cat 1) and run basic math? My goal is to take the most recent comment's input from each post and add it together and display it via some php/html on another page/post or widget somewhere. Thanks!

+1  A: 

If I'm understanding you right:

// http://codex.wordpress.org/Function_Reference/query_posts
$args = array(
    'cat' => 1,
    'orderby' => 'date',
    'order' => 'desc',
    'posts_per_page' => -1,
);
// get all (-1) posts in category 1
$posts = query_posts($args);

// variable to hold our basic sum
$sum = 0;
foreach($posts as $post) {
    $post_id = $post->ID;
    // http://codex.wordpress.org/Function_Reference/get_comments
    $comment_args = array(
        'post_id' => $post_id,
        'status' => 'approve',
        'orderby' => 'comment_date_gmt',
        'order' => 'DESC',
        'number' => 1,
    );
    // get the most recent approved comment by post_id
    $comments = get_comments($comment_args);
    foreach($comments as $comm) {
        // set the integer value of the comment's content
        $integer_comment_value = (int)$comm->comment_content;
        // add it to the sum
        $sum += $integer_comment_value;
    }
}
// print the sum
print $sum;
artlung
This worked perfect! I ended up implementing it as a wordpress admin dashboard widget so that it's "eyes only" for the people running the site!
RodeoRamsey
Aha! Very cool!
artlung