Sort Comments by "like_amount" Meta Key in WordPress

This code modifies the default WordPress comment query to sort comments by a custom meta key like_amount. Comments with higherlike_amountvalues are displayed first, while comments without this meta key are included and placed at the bottom. The secondary sorting criterion iscomment_date_gmtin ascending order.

How It Works:

  1. Target Specific Queries:
    • Applies only to the main comments query on singular pages (is_singular()).
    • Does not run in the WordPress admin area (!is_admin()).
  2. Include All Comments:
    • Uses ameta_querywith anORcondition:
      • Includes comments with thelike_amountmeta key (EXISTS).
      • Includes comments without the meta key (NOT EXISTS).
  3. Custom Sorting:
    • Primary sorting:meta_value_num(numeric value oflike_amount) in descending order.
    • Secondary sorting:comment_date_gmt(comment date) in ascending order.
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us