How to display the “ratingValue” and “ratingCount” values ​generated by the KK Star Ratings plugin into my page’s recipe ld+json schema

Question

I’m trying to add ld+json schema markup to each recipe page on my site that will automatically pull over the most up to date aggregator rating data (from kk star ratings). I came across this post (https://wordpress.stackexchange.com/questions/368872/show-the-ratingvalue-and-ratingcount-values-of-kk-star-ratings-plugin) which has the below code:

//Getter methods

//this will return the base best score like 5 or 10.
function get_best_rating(){
    return max((int) get_option('kksr_stars'), 1);
}


//This will return the rating vote count
function get_rating_count($id){
    return count_filter(null, $id, null);
}

//This will return the rating score value
function get_rating_score($id){
    return score_filter(null, $best, $id, null);
}

//Helper Functions
function count_filter($count, $id, $slug){
    if ($slug) {
        return $count;
    }

    $count = (int) get_post_meta($id, '_kksr_casts', true);

    return max($count, 0);
}

function score_filter($score, $best, $id, $slug){
    if ($slug) {
        return $score;
    }

    $count = count_filter(null, $id, null);
    $counter = (float) get_post_meta($id, '_kksr_ratings', true);

    if (! $count) {
        return 0;
    }

    $score = $counter / $count / 5 * $best;
    $score = round($score, 1, PHP_ROUND_HALF_DOWN);

    return min(max($score, 0), $best);
}

Now you can call the getter function like this:

$best = get_best_rating();
$count = get_rating_count(get_the_id()); //You must provide post id as param
$score = get_rating_score(get_the_id()); //You must provide post id as param

I’m not sure what I’m doing wrong. I’ve added the first piece of code to my child theme’s functions.php file. Then I’ve added the calls coding to my ld+json schema markup which I have embedded onto the specific page (see below).

    <script type="application/ld+json">
{
  "@context": "http://schema.org",
  "Newtype": "Recipe",
  "recipeCategory": "Drinks",
  "name": "Apple Pie Moonshine Recipe",
  "url": "https://milehidistilling.com/apple-pie-moonshine-recipe/",
  "author": {
          "Newtype": "Organization",
          "name": "Mile Hi Distilling"},
    "aggregateRating": {
        "Newtype": "AggregateRating",
        "ratingValue": "$score = get_rating_score(get_the_id(125070));",
        "bestRating": "$best = get_best_rating();",
        "ratingCount": "$count = get_rating_count(get_the_id(125070));"},
  "prepTime": "PT5M",
  "cookTime": "PT60M",
  "totalTime":"PT90M",
  "recipeYield": "4 quarts",
  "recipeCuisine":"American",
    "nutrition": {
    "Newtype": "NutritionInformation",
    "servingSize": "4 quarts",
    "calories": "104 calories"
    },
  "datePublished": "2019-07-27",
  "description": "In this guide, we'll be explaining two different step-by-step recipes for making a delicious batch of Apple Pie Moonshine.",
  "keywords": "apple pie moonshine, apple pie moonshine recipe, moonshine recipe, moonshine recipes",
  "image": [
  "https://milehidistilling.com/wp-content/uploads/photos/1x1/Apple-Pie-Moonshine.jpg",
  "https://milehidistilling.com/wp-content/uploads/photos/4x3/Apple-Pie-Moonshine.jpg",
  "https://milehidistilling.com/wp-content/uploads/photos/16x9/Apple-Pie-Moonshine.jpg"
],
  "recipeIngredient": [
    "8 cups of moonshine (120 proof) or vodka (80 proof) if you want a less potent final product",
    "5 cans of apple juice concentrate",
    "3-4 cups of brown sugar",
    "10 cinnamon sticks",
    "Apple pie spice",
    "Dried ginger",
    "Vanilla bean",
    "1 apple"
  ],
  "recipeInstructions":[{"Newtype":"HowToStep","text":"1. For the first step, you pour all 5 cans of apple juice concentrate into a large pot."},{"Newtype":"HowToStep","text":"2. Add 3-4 cups of brown sugar (depending on how sweet you want your batch to turn out)."},{"Newtype":"HowToStep","text":"3. Next, add in your 10 cinnamon sticks, cut your apple into 4 pieces and add them into the mix as well.  If you’d like to create a more complex flavor profile for your apple pie flavored moonshine you can also add 1 pinch of apple pie spice, 1/4 cup of dried ginger, and 1-2 sticks of vanilla bean into the batch.  Be aware that a small amount of each of these goes a long way!"},{"Newtype":"HowToStep","text":"4. Bring your mixture to a low simmer. Cover your pot with a lid and reduce heat. Let your mixture simmer for one-hour. After that, remove your pot from the burner and allow the mixture to cool down."},{"Newtype":"HowToStep","text":"5. Once the mixture cools down, you can pour in your 8 cups of moonshine.  *Be sure to wait until the mixture cools down before pouring in your moonshine.  Stir to completely mix the moonshine in with your apple concentrate batch."},{"Newtype":"HowToStep","text":"6. Next slowly pour your moonshine mixture into your 4 quart jars.  Be sure to use a cheesecloth to filter the moonshine/apple concentrate mixture as you pour it.  Add 1 cinnamon stick and 1/4th of an apple that was previously in your mixture to each jar."},{"Newtype":"HowToStep","text":"7. Congrats! You can enjoy your apple pie flavored moonshine immediately. For a more complex and aged flavor, seal and store the jars in your refrigerator for 2-3 weeks before you enjoy your batch. Be sure to stir up the mix well before serving to avoid an uneven pour of shine."}]
}
</script>

All I get when I check my structured data is:
enter image description here
So I think this tells me that the functions.php code isn’t pulling that kk star ratings data correctly or I’m not calling the data correctly with the code I have in my ld+json schema. Either way it’s not proving the actual aggregator rating variables for the recipe schema.

KK Star Ratings is already dynamically producing a separate schema markup as a CreativeWorkSeries with the specific variables for Aggregator rating (ratingValue, bestRating, ratingCount). I just want to target those same dynamically loaded variables for my recipe schema markup. I hope I have explained this clearly. Thanks to anyone who can help me out with this. Let me know if I can clarify any details.

0
Kyle Doran 1 month 0 Answers 5 views 0

Leave an answer