Example of executing code after first post to a thread?

Kevin

Well-known member
Any examples of executing some code after the first, and only the first, post of a thread? Essentially I just want to have some code kick in after somebody replies to an existing thread for the first time and if that post response is not flagged for spam, etc..

If there are any examples I could check out, that'd be really appreciated, thanks. :)
 
Hi
I haven't tried it, but try setting up a code listener (entity_post_save), and running your code only if
$post->Thread->first_post_id == $post->post_id

PHP:
class PostListener
{
    public static function postSave(Entity $post)
    {
        // Ensure the entity is a Post
        if (!$post instanceof Post) {
            return;
        }

        if ($post->isInsert()) { // New post was created
            if ($post->Thread->first_post_id == $post->post_id){
                self::callYourFunction();
            }
        }
    }
    
    protected static function callYourFunction() {
        /* Your code */
    }
}
 
Last edited:
I haven't tried it, but try setting up a code listener (entity_post_save), and running your code only if
$post->Thread->first_post_id == $post->post_id
Thanks, @Orit, you've set me on the right path. I need to finish up redoing & testing my code but my objective is in sight.

Because the first_post_id of the thread entity is actually post #1 of the thread (as opposed to the first reply to the thread), here's what I ended with.
Code:
if ($post->Thread->reply_count == '1' && $post->Thread->last_post_id == $post->post_id)
{
     // Do Stuff Here :)
}
 
Back
Top Bottom