author – Counting instances of words in the results of a post query


I’m way over my head with this one and wonder if anyone can shine a light on where I’m going wrong.

I am grabbing some authormeta for the resulting posts of a query, and I want to tally up the number of times certain authormeta is repeated in those results.

E.g. There are 10 ‘recipe’ posts > The author of each post (different for each one) has a ‘favourite food’ meta item > I want the result to be something like “3 recipe authors have ‘pie’ as their favourite food”.

Here’s what I’ve done so far, which has gotten me close, but I’d need the results of $authorfood to be used in $text but it seems it only works with a plain text statement?

$the_query = new WP_Query( array( 'post_type' => 'recipe', 'posts_per_page' => -1 ) );
 if ( $the_query->have_posts() ) :
  while ( $the_query->have_posts() ) :
   $authorfood = get_the_author_meta( 'favourite_food' ); // the result of this is the plain list I want to use later, e.g. "appleappleappleapplebananapiepiepie"
   $text = "Plain text to count frequency of words"; // this needs to be the result of $authorfood above
   $words = str_word_count($text, 1);
   $word_frequencies = array_count_values($words);

I’m probably going about this completely the wrong way. Sorry for the stupid question, but I’ve searched and searched and can’t find an answer.

Any ideas how I can tally the number of ‘favourite foods’ of ‘recipe authors’ (it has to be specifically those post authors, rather than all authors on the site, as there are multiple post types)?

Lorn Waterfield 1 month 2021-05-10T11:12:39-05:00 0 Answers 0 views 0

Leave an answer