1

I have two custom fields for each post, vote_up and vote_down.

I know how to order by one of them, but I'm wondering how I can get the totals of both, subtract vote_down from vote_up and then display them according to the result of that calculation.

Ideas?

3
  • 2
    The easiest way might be to create a third custom field, vote_diff, and order by that. You could use a update_post_meta in a function to automatically calculate/enter the value of vote_diff when vote_up or vote_down changes. Where & how to do that will depend on where/how vote_up and vote_down are currently added/updated (plugin or theme or ....?).
    – Michelle
    Nov 10, 2016 at 16:23
  • @Michelle as an answer please :)
    – Rarst
    Nov 10, 2016 at 16:36
  • @Rarst done! I didn't know if I'd given enough detail to merit an actual answer but if you say so, it is so! :)
    – Michelle
    Nov 10, 2016 at 16:40

1 Answer 1

2

Per Rarst's request, as an answer instead of comment :-):

The easiest way might be to create a third custom field, vote_diff, and order by that. You could use update_post_meta in a function to automatically calculate/enter the value of vote_diff when vote_up or vote_down changes. Where & how to do that will depend on where/how vote_up and vote_down are currently added/updated (plugin or theme or ....?).

1
  • Bingo! I figured it out about a minute. :)
    – Nathan
    Nov 10, 2016 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.