What I understand from your question is that you want to:
Count how many times the words inside a certain user meta field are repeated accross all the meta results for a query.
One way to achieve this is to put all your meta values together into one string and then count the repeated words in that new string.
// Create a string with all favorite foods
$all_favorites="";
while($the_query->have_posts()){
$the_query->the_post();
$authorfood = get_the_author_meta('favourite_food');
if(strlen($authorfood)>0){
$all_favorites .= ' '.$authorfood;
}
}
// Count
$words = explode(' ', $all_favorites);
$counts = array();
foreach($words as $word){
$word = preg_replace("#[^a-zA-Z\-]#", "", $word);
$counts[$word] += 1;
}
Then, the $counts
variable will be an array with the structure (key) "food" => (value) count
for all the words that appear in your site’s favourite_food
user meta values.
Note: This will become heavy once you reach a high number of users. A better approach would be to save the user’s favorite food (or foods) as independent entities with unique ids where you can then easily count things.