Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Latest Activity in a category WordPress

  • SOLVED

I would like to display the latest activity in a category. It can be a new post or a new comment. I am trying to dipslay the forums like a vbulletin forum software.


I would like to display it as

Post Name (link to post or comment)
by post author or comment author (link to author profile/page)


It should display the post author if there are no comments and if there are any comments, it should display the comment author.

Answers (4)

2010-07-13

Oleg Butuzov answers:

bbPress?


Ant Eksiler comments:

I dont want plugin or standalone solutions


Oleg Butuzov comments:

http://wordpress.org/extend/plugins/get-recent-comments/


Oleg Butuzov comments:

as for posts for example from category one ....

<?php query_posts('cat=1'); ?>
...

an so on...


Oleg Butuzov comments:

http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/The_Loop
http://www.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/


Ant Eksiler comments:

I know those bits but I would like to display a single bit showing the latest post or comment.

For example, if there is a new comment on a post 2 years old. The latest activity will be this comment. Not a post that is made a day ago.

As you can see, latest activity requires the comment time and post times are compared or stored in another variable.


Oleg Butuzov comments:

i know that you don't want a plugin, but still check the
http://wordpress.org/extend/plugins/get-recent-comments/

its work better than stickct sql. there no conditional tegs of wordpress for it.

instead using simple sql queries whan you will need to select categories that are nested by parent (that you will select), and select allposts ids that are in your categories, and only after that select comments that are connected to the posts from your categories.

well. better to use plugin.


Oleg Butuzov comments:

and yeah that plugin provide a code that you can easy add to please where you want to show the last comments. its not overloading front end.


Ant Eksiler comments:

I don't want to display latest comments?


Oleg Butuzov comments:

i asume you want to do it =). but one of your comments tell me that

<blockquote>I dont want plugin or standalone solutions</blockquote>...

check the plugin =)


Ant Eksiler comments:

I don't need the plugin because I can get the recent comments using:

$comments = get_comments('status=approve&number=4');


Latest Activity in a category means more advanced coding.


Oleg Butuzov comments:

didn't you wanted to get it fro specifyc category?


Ant Eksiler comments:

Please read my question again because I can certainly google "wordpress recent comments"


Oleg Butuzov comments:

// parent category
$parentID = 66;

//cateogries
$categories = get_terms('category', array('child_of' => $parentID, 'hide_empty' => false));
$categryList = array($parentID);
foreach($categories as $term){
$categryList[] = (int) $term->term_id;
}

// posts
$posts = get_objects_in_term($categryList, 'category');

// last activity
$sql = "
SELECT
'post' as type,
p.ID as id,
p.post_date as date,
p.post_title as content,
u.user_nicename
FROM
{$wpdb->posts} p,
{$wpdb->users} u
WHERE
p.ID in (".implode(',', $posts).") AND
p.post_author = u.ID
UNION SELECT
'comment',
c.comment_ID,
c.comment_date ,
c.comment_content ,
c.comment_author
FROM {$wpdb->comments} c
WHERE
c.comment_post_ID in (".implode(',', $posts).")
ORDER by date DESC LIMIT 10
";
$data = $wpdb->get_results($sql);


change parentID for category...
$data is yours!

cheers!


Oleg Butuzov comments:

with output

// parent category
$parentID = 66;

//cateogries
$categories = get_terms('category', array('child_of' => $parentID, 'hide_empty' => false));
$categryList = array($parentID);
foreach($categories as $term){
$categryList[] = (int) $term->term_id;
}

// posts
$posts = get_objects_in_term($categryList, 'category');

// last activity
$sql = "
SELECT
'post' as type,
p.ID as id,
p.post_date as date,
p.post_title as content,
u.user_nicename as user
FROM
{$wpdb->posts} p,
{$wpdb->users} u
WHERE
p.ID in (".implode(',', $posts).") AND
p.post_author = u.ID
UNION SELECT
'comment',
c.comment_ID,
c.comment_date ,
c.comment_content ,
c.comment_author
FROM {$wpdb->comments} c
WHERE
c.comment_post_ID in (".implode(',', $posts).")
ORDER by date DESC LIMIT 10
";
$data = $wpdb->get_results($sql);



echo '<ul>';
foreach($data as $item){
switch($item->type){
case 'comment':
?>
<li class='comment'><a href="<?php echo get_comment_link($item->id); ?>"><?php echo apply_filters('the_title', substr($item->content,0,255)); ?></a><br /> by <?php echo $item->user; ?> </li>
<?

break;
case 'post':
?>
<li class='post'><a href="<?php echo get_permalink($item->id); ?>"><?php echo apply_filters('the_title', substr($item->content,0,255)); ?></a><br /> by <?php echo $item->user; ?> </li>
<?
break;
}
}
echo '</ul>';

2010-07-13

Lew Ayotte answers:

Try this (I haven't tested it yet):


//Get your latest post in a category:
$args = array('cat' => 4,
'showposts' => 1,
'orderby' = 'date');
query_posts($args);

if (have_posts()) : while (have_posts()) : the_post();
$args = array('post_ID' => $post->ID,
'status' => "approve",
'order' => 'ASC',
'number' => 1);
$comment = get_comments($args);

if(!empty($comment)) {
?>
<a href="<?php get_comment_link($comment[0]->comment_ID) ?>"><?php the_title(); ?></a>
by <a href="<?php echo esc_url($comment[0]->comment_author_url;) ?>"><?php $comment[0]->comment_author; ?></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
by <?php the_author() ?>
<?php
}

endwhile; endif;


Lew


Ant Eksiler comments:

Thats a good start and I have thought of it before but that is true for the last post only.

What if a user makes a comment on a post that is older? You script wont see that.


Lew Ayotte comments:

Ah, good point... let me rethink...


Lew Ayotte comments:

This seems to work... except the comment link, still trying to figure that one out:


$args = array('cat' => 4,
'showposts' => 1,
'orderby' => 'date' );
$latest = new WP_Query($args);

$latest->the_post();
//print_r($post);

$args = array('status' => "approve",
'order' => 'ASC',
'number' => 1);
$comment = get_comments($args);
//print_r($comment);

if (strtotime($comment[0]->comment_date_gmt) > strtotime($post->post_date_gmt)) { ?>
<a href="<?php get_comment_link($comment[0]->comment_ID); ?>"><?php the_title(); ?></a><br />
by <a href="<?php echo esc_url($comment[0]->comment_author_url); ?>"><?php echo $comment[0]->comment_author; ?></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a><br />
by <?php the_author() ?>
<?php }


Lew Ayotte comments:

Oh wait, that's not for a comment in a specific category...


Lew Ayotte comments:

Try this... everything *should* be working now :)

$args = array('cat' => 4,
'showposts' => 1,
'orderby' => 'date' );
$latest = new WP_Query($args);

$latest->the_post();
//print_r($post);

$args = array('status' => "approve",
'order' => 'ASC' );
$comments = get_comments($args);
foreach ($comments as $comment) {
if (in_category(4, $comment->comment_post_ID)) {
$latest_comment = $comment;
break;
}
}
//print_r($comment);

if (strtotime($latest_comment->comment_date_gmt) > strtotime($post->post_date_gmt)) { ?>
<a href="<?php echo get_comment_link($latest_comment->comment_ID); ?>"><?php the_title(); ?></a><br />
by <a href="<?php echo esc_url($latest_comment->comment_author_url); ?>"><?php echo $latest_comment->comment_author; ?></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a><br />
by <?php the_author() ?>
<?php }


Lew Ayotte comments:

The comment arg should be DESC not ASC:

$args = array('cat' => 4,
'showposts' => 1,
'orderby' => 'date' );
$latest = new WP_Query($args);

$latest->the_post();
//print_r($post);

//echo "<hr>";

$args = array('status' => "approve",
'order' => 'DESC' );
$comments = get_comments($args);
foreach ($comments as $comment) {
if (in_category(4, $comment->comment_post_ID)) {
$latest_comment = $comment;
break;
}
}
//print_r($comments);

//echo "<hr>";

if (strtotime($latest_comment->comment_date_gmt) > strtotime($post->post_date_gmt)) { ?>
<a href="<?php echo get_comment_link($latest_comment->comment_ID); ?>"><?php the_title(); ?></a><br />
by <a href="<?php echo esc_url($latest_comment->comment_author_url); ?>"><?php echo $latest_comment->comment_author; ?></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a><br />
by <?php the_author() ?>
<?php }

2010-07-13

Rashad Aliyev answers:

which forum?


Ant Eksiler comments:

I meant category :)

2010-07-13

Darrin Boutote answers:

A few questions:

Where did you want to display the information?

What are you using to power your forums? bbPress?

Do you have an example site? I just integrated bbPress into a WordPress-powered website and had to do something similar. The home page shows Latest Discussions from the forums: http://www.fundamentalrental.com/


Ant Eksiler comments:

Sorry I changed the question now.

I would like to use wordpress like a forum where I need the latest activity in a category.

I don't want to use bbPress as I want to code the rest myself.


Darrin Boutote comments:

After spending far too long coming up with a solution, Lew's is by far the most elegant. Except, after testing there were some small tweaks.

Specifically, he had:
<a href="<?php echo get_comment_link($latest_comment->comment_ID); ?>"><?php the_title(); ?></a><br />

Whereas it should be:
<a href="<?php echo get_comment_link($latest_comment->comment_ID); ?>"><?php echo get_the_title($latest_comment->comment_post_ID); ?></a><br />

When I tested it, it was originally pulling the latest post title, not the post title of the latest comment.

Also, Ant wanted a link to the author. The template tag the_author() only displays the name, not a link. For that you need the_author_link().

So this line:
by <?php the_author() ?><code>

Should be changed to:
<code>by <?php the_author_link(); ?>


Here's the revised, neatened code:


<?php
$cat_id = 30; //Change ID to your category id

$args = array('cat' => $cat_id,
'showposts' => 1,
'orderby' => 'date' );

$latest = new WP_Query($args);
$latest->the_post();

$args = array('status' => "approve",
'order' => 'DESC' );

$comments = get_comments($args);

foreach ($comments as $comment) {
if ( in_category($cat_id, $comment->comment_post_ID) ) {
$latest_comment = $comment;
break;
}
}

if ( strtotime($latest_comment->comment_date_gmt ) > strtotime($post->post_date_gmt)) { ?>
<a href="<?php echo get_comment_link($latest_comment->comment_ID); ?>"><?php echo get_the_title($latest_comment->comment_post_ID); ?></a><br />
by <a href="<?php echo esc_url($latest_comment->comment_author_url); ?>"><?php echo $latest_comment->comment_author; ?></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
by <?php the_author_link(); ?>
<?php } ?>


Great script Lew!


Darrin Boutote comments:

I have no idea why the comment form just forked my previous comment, but here's the revised code:


<?php
$cat_id = 30; //Change ID to your category id

$args = array('cat' => $cat_id,
'showposts' => 1,
'orderby' => 'date' );

$latest = new WP_Query($args);
$latest->the_post();

$args = array('status' => "approve",
'order' => 'DESC' );

$comments = get_comments($args);

foreach ($comments as $comment) {
if ( in_category($cat_id, $comment->comment_post_ID) ) {
$latest_comment = $comment;
break;
}
}

if ( strtotime($latest_comment->comment_date_gmt ) > strtotime($post->post_date_gmt)) { ?>
<a href="<?php echo get_comment_link($latest_comment->comment_ID); ?>"><?php echo get_the_title($latest_comment->comment_post_ID); ?></a><br />
by <a href="<?php echo esc_url($latest_comment->comment_author_url); ?>"><?php echo $latest_comment->comment_author; ?></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
by <?php the_author_link(); ?>
<?php } ?>