• Resolved marcu5p

    (@marcu5p)


    Hi

    I have a requirement to reindex a post when that post has a comment added.

    Please could you give me some guidance on achieving this.

    Thanks as always
    Marcus

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hi @marcu5p

    I **think** something like https://gist.github.com/tw2113/81fc956c7e179e19794887d0f8391bc4 should suffice.

    I know in the code testing, it wasn’t reaching the catch block towards the end, but I don’t have anything immediate that would change in the post, upon a comment. Not sure what you’re updating as part of that process for the post, when a comment comes in.

    Only idea I had for that is you’re including comment count in the indexed objects.

    Give the code a try and let us know if it works out for you.

    Thread Starter marcu5p

    (@marcu5p)

    Hi Michael

    Thanks for your swift reply as always.

    You’re right I am wanting to index the comment count, and your function works a dream so thanks for that.

    I have also implemented a simple star star rating for comments (a version of this https://www.cssigniter.com/add-rating-wordpress-comment-system/) and called the ci_comment_rating_get_average_ratings function in an attempt to also index the average rating at the same time. It nearly works but indexes the ratings previous value which I guess is down to not reaching reaching the catch block.

    Any ideas on solving this bit would be much appreciated.

    Thanks
    Marcus

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Have an even swifter reply as I was going through my morning support queue.

    I have to suspect it’s a matter of order of operation here. The metadata saving is probably happening after the run of wp_insert_comment so we’re getting it right before, when we need it right after.

    Assuming you’ve kept the ci_comment_rating_save_comment_rating() function largely the same, then something like this below would probably work. Instead of working on the wp_insert_comment hook, we run our re-index logic after you’ve updated the meta. Biggest difference from my original one is that we have to do a get_comment() because this filter isn’t passed the comment object, just the ID.

    https://gist.github.com/tw2113/737b24671df4340e96c52f4ae106f710

    Thread Starter marcu5p

    (@marcu5p)

    Hi Michael

    Wow, lightning fast reply, thank you.

    That works a treat, and of course makes perfect sense to modify the ci_comment_rating_save_comment_rating() instead.

    Both elements now working, only thing left to say is… you’re a star!

    Thanks as always
    Marcus

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Reindex post when a comment is added’ is closed to new replies.