How does this WordPress Plugin (Thrive Comments) apply their custom comment sort? [closed]

How the JS script works

  1. On page load, the script loads the post comments by making an AJAX request to a custom WordPress REST API endpoint at /tcm/v1/comments/<post ID> (here’s a sample URL, valid as of writing).

  2. Then the script displays the comments sorted by the sort_field value in the comment localization parameters defined in the tcm_get_localization_parameters() (PHP) function. So at this point, your custom sort field (the metadata named confidence_rank_cached) would work as expected.

  3. However, it didn’t work when you selected the “Top Rated” option in the dropdown because the script (always) sets back the sort field to comment_karma — see the relevant code below:

    sortComments: function (a) {
        switch (a) {
            ...
            case "top-rated":
                (this.collection.compField = "comment_karma"), (this.collection.compOrder = -1);
        }
        ...
    },
    

How to make your custom sort field works when the “Top Rated” option is selected

  1. Change the above comment_karma to confidence_rank_cached.

    Yes, modifying (core) plugin files is not recommended, but I don’t have enough time to look thoroughly on the plugin script. Hence it’s up to you to come up with a better solution than modifying the original JS file.

  2. Add your custom metadata to the REST API endpoint mentioned above — and add the metadata as a root/top-level property (i.e. same level as the comment_karma field).

    I don’t know if the plugin provides a specific hook for filtering the comment objects/data in the REST API response, but you can try using the get_comment hook to add the custom metadata to the comment object.