logo

$20
Latest Activity in a category

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.

Ant Eksiler | 07/13/10 at 9:17am | Edit


(4) Possible Answers Submitted...

  • avatar
    Last edited:
    07/13/10
    12:48pm
    Oleg Butuzov says:

    bbPress?

    Previous versions of this answer: 07/13/10 at 9:24am | 07/13/10 at 9:33am

    • 07/13/10 9:34am

      Ant Eksiler says:

      I dont want plugin or standalone solutions

    • 07/13/10 9:36am

      Oleg Butuzov says:

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

    • 07/13/10 9:39am

      Oleg Butuzov says:

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

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

      an so on...

    • 07/13/10 9:40am

      Oleg Butuzov says:

      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/

    • 07/13/10 10:00am

      Ant Eksiler says:

      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.

    • 07/13/10 10:05am

      Oleg Butuzov says:

      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.

    • 07/13/10 10:06am

      Oleg Butuzov says:

      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.

    • 07/13/10 10:17am

      Ant Eksiler says:

      I don't want to display latest comments?

    • 07/13/10 10:22am

      Oleg Butuzov says:

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

      I dont want plugin or standalone solutions
      ...

      check the plugin =)

    • 07/13/10 10:27am

      Ant Eksiler says:

      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.

    • 07/13/10 10:28am

      Oleg Butuzov says:

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

    • 07/13/10 10:34am

      Ant Eksiler says:

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

    • 07/13/10 11:07am

      Oleg Butuzov says:

      	// 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!

    • 07/13/10 11:13am

      Oleg Butuzov says:

      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>';

  • avatar
    Last edited:
    07/13/10
    9:25am
    Rashad Aliyev says:

    which forum?

    • 07/13/10 9:34am

      Ant Eksiler says:

      I meant category :)

  • avatar
    Last edited:
    07/13/10
    9:25am
    Darrin Boutote says:

    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/

    • 07/13/10 9:36am

      Ant Eksiler says:

      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.

    • 07/13/10 12:14pm

      Darrin Boutote says:

      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() ?>


      Should be changed to:
      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!

    • 07/13/10 12:17pm

      Darrin Boutote says:

      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 } ?>

  • avatar
    Last edited:
    07/13/10
    12:48pm
    Lew Ayotte says:

    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

    Previous versions of this answer: 07/13/10 at 10:04am | 07/13/10 at 10:05am

    • 07/13/10 10:19am

      Ant Eksiler says:

      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.

    • 07/13/10 10:39am

      Lew Ayotte says:

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

    • 07/13/10 10:52am

      Lew Ayotte says:

      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 }

    • 07/13/10 10:53am

      Lew Ayotte says:

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

    • 07/13/10 10:59am

      Lew Ayotte says:

      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 }

    • 07/13/10 11:04am

      Lew Ayotte says:

      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 }

This question has expired.





Current status of this question: Completed