How do I add the values of meta fields in the loop?

Say each post has a custom field called "rating" (or whatever) and its got a value of 1 to 5 and its kept in a custom post meta.

In the loops when Im going through all the post, how would I add up all the post metas together to get a total "rating" value?

  • 1
    Is this really your question, if you can get the values of the post meta in a loop, cannt you just add those ?? – Maruti Mohanty Dec 18 '13 at 18:36
  • @JonFury as I suggested in this comment on another of your questions, please conduct research through other channels before posing the question on WPSE. This question, too, would have been quickly answered by both Google and the WordPress codex. I strongly recommend that you read the codex entry on Writing a Plugin and that you spend some time browsing the Function Reference. – bosco Jan 1 '14 at 22:43

You can put the meta value of each post within an array while inside loop. Then just calculate the average of the ratings after the loop ends. for getting meta values you can see this link Function Reference/get post meta

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.